Reputation: 1975
When I'm declaring instance methods in JS I often use this syntax:
var MyObj = function (p1) {
this.p1 = p1;
};
MyObj.prototype = {
method1: function() {},
method2: function() {},
method2: function() {}
};
Is there a similar way to declare "static" methods instead of doing this:
MyObj.static1 = function() {
};
MyObj.static2 = function() {
};
MyObj.static3 = function() {
};
Upvotes: 2
Views: 63
Reputation: 10849
The only thing I can think of is doing it in two steps:
var staticMethods = {
static1: function () {
},
static2: function () {
},
static3: function () {
}
};
Then use this function
function registerStaticMethods(aClass, staticMethods) {
for (var methodName in staticMethods) {
aClass[methodName] = staticMethods[methodName];
}
}
You would use it like this
registerStaticMethods(MyObj, staticMethods);
Upvotes: 2