Re:How Javascript Prototypal Inheritance Really works
by jashkenas on Saturday May 28, 2011 @04:13PM
(#2595118)
My feelings are that "new" is really what you want. If you're thinking, "I want to make a new point at 20, 100", you probably want to write:
new Point(20, 100);
The problem in JS isn't the "new" keyword, it's how awkward and brittle prototypes are to create. You have to know to first define a constructor function (read "class object"), and then attach prototypal properties to it one at a time. If you intend to use the prototype chain for inheritance, you have to be even more careful, and bend over backwards to configure the prototype chain correctly, which requires the creation of a temporary intermediate function.See later in this thread: http://news.ycombinator.com/item?id=2595128 ... nasty stuff. Verbose, fragile, and confusing.
This is why the ECMAScript committee is hard at work on proposals like these:
http://wiki.ecmascript.org/doku.php?id=strawman:classes
http://wiki.ecmascript.org/doku.php?id=strawman:classes_as_i...
http://wiki.ecmascript.org/doku.php?id=strawman:classes_as_s...
http://wiki.ecmascript.org/doku.php?id=strawman:classes_with...
http://wiki.ecmascript.org/doku.php?id=strawman:classes_with...
There's a glimmer of hope though. In Firefox 4.0.1 Object.create is almost as fast as new Constructor. If other implementations caught up in this respect what are your feelings on new vs. Object.create?