I want to add a static function to a class in EcmaScript 5 JavaScript. My class definition looks 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 Account
class. If I had created the Account
class using a constructor function and the prototype
property 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 am using Object.defineProperty
and not the prototype
property to add members, Account.instances
and Account.createInstance
would also be instantiated when calling Object.create
and therefore be properties of the instance.
How do i add a static member to a class when using EcmaScript 5 style object creation?