I want to add a static function to a class in JavaScript in ExmaScript 5. The definition of my class is as follows:
var Account = {};
Object.defineProperty(Account, 'id', {
value : null
});
And I would create a new instance like this:
var theAccount = Object.create(Account);
theAccount.id = 123456;
Now I want to add a static function to the class Account. If I created a class Accountusing the constructor function and property prototype, like this:
var Account = function () {
this.id = null;
};
... I could just do:
Account.instances = {};
Account.createInstance = function () {
var account = new Account();
account.id = uuid.v4();
Account.instances[account.id] = account;
return account;
};
But since I use Object.defineProperty, not a property prototypeto add members, Account.instancesthey are Account.createInstancealso created when called, Object.createand therefore are instance properties.
How to add a static member to a class when using EcmaScript 5 style object creation?
source
share