4 ms·
So you basically just came up with what Crockford calls the 'Functional' method of creating objects? Why duplicate effort in describing the concept?
by TazeTSchnitzel 15y ago
So you basically just came up with what Crockford calls the 'Functional' method of creating objects?
Why duplicate effort in describing the concept?
- J3L2404 15y agoI find it a continuing source of confusion that people can't distinguish between a person who submits an item, and the author of the item being submitted. I submitted this item. I did not write it. The person who wrote it did not submit this to HackerNews.
- TazeTSchnitzel 15y agoI realised that when I posted it. There doesn't seem to be a comments field on the site, unfortunately. So this only serves as a heads-up to other people on Hacker News.
- J3L2404 15y agoYou "realized that" but still used "you". I don't understand.
- gabordemooij 15y agoNo, 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