4 ms·
No, it isnt. It's cleaner because the class-object is an object instead of a function, this makes it possible to add static methods.
by gabordemooij 15y ago
No, it isnt. It's cleaner because the class-object is an object instead of a function, this makes it possible to add static methods.
- stephenr 15y agoYou can have "static methods" on a prototype constructor. Functions are objects, so adding a static property to a constructor function is no harder than adding it to an object literal.
- gabordemooij 15y agoYes it is, because you can not add them in the function body: cls = function(){} cls.staticMethod = function(){} vs cls = { staticMethod:function(){} } I prefer the latter because the static method can be defined within the body of the 'class' definition. Using the former method it's too easy to add static methods in different places.
- stephenr 15y agoBy definition the concept provided means you can't have "static methods". You get an instance of your "class" by calling createNew(), which returns an object literal it just created. This object literal has methods attached to it, but you can't call them before you "instantiate" an "instance" by calling createNew(), so you don't have any static methods at all - just a heap of "instance methods" that may or may not operate on the instance. Using a Prototype "class" you can very easily have true "static" and "instance" methods, that are called without and with an instance respectively. Also, I have no idea what you mean by "add static methods in different places"?? You either: * add a method to the constructor directly, which makes it static FooConstructor.method = function () {...};) * or you add it to the constructor's prototype, which makes it an instance method FooConstructor.prototype.method = function () {...};
- gabordemooij 15y agoI mean that with the minimalist approach you can define static methods in your 'class' body, thus grouping them neatly together: MyClass = { getInstance: function(){ ... } } vs MyClass = function(){ } //outside function body... MyClass.prototype.getInstance = function(){ ... }
- stephenr 15y agoMyClass = function(){ } //outside function body... MyClass.prototype.getInstance = function(){ ... } but this makes no sense - why would you have a "static" method defined as an instance method? in this example you would do: var MyClass = function (){}; // constructor MyClass.getInstance = function (){}; // static method MyClass.prototype.start = function (){}; // instance method
- gabordemooij 15y agoI think the nested notation is simply more maintainable. MyClass = { staticMethod: function(){ } createNew: function() { obj = {}; obj.instanceMethod = function(){} return obj; } } Also, I find it quite elegant to see that the constructor function (factory method) is implemented as a static method as well). Of course I know these aren't static methods like in Java/PHP but in practice they can treated as such.
- stephenr 15y ago> I think the nested notation is simply more maintainable why though? I don't see how being part of an object literal is inherently more maintainable? > Also, I find it quite elegant to see that the constructor function (factory method) is implemented as a static method as well) but you were harping on about "OOP standards" and "best practices" before - since when is the constructor for a class a static method? In a language with true classes, even with a Singleton, the constructor is an instance method.
- 15y ago