3 ms·
> 1) The use of the pattern is not common in the js community. The use of prototypes is ubiquitous in the JS community. There's no two ways about it. Unfortu
by jashkenas 15y ago
> 1) The use of the pattern is not common in the js community.
The use of prototypes is ubiquitous in the JS community. There's no two ways about it.
Unfortunately, the avoidance of prototypes is also a common anti-pattern in the JS community, simply because JavaScript prototypes are so awkward, fragile and verbose. Doing the wrong thing with JS prototypes is easier than doing the right thing, which leads many developers to simply throw up their hands, and write code like this instead:
function makeHorse() {
var horse = {
walk: function(){ ... },
trot: function(){ ... },
canter: function(){ ... },
gallop: function(){ ... }
};
return horse;
};
... which is terribly inefficient and wasteful of memory, and accomplishes nothing that a prototype couldn't do in a small fraction of the space and time.
- MatthewPhillips 15y agoObviously the use of prototypes are ubiquitous in JavaScript. No one said otherwise. The use of class inheritance, as is common in other languages, like Java, C#, Python, and CoffeeScript is not common at all. Isn't that why you built it into CoffeeScript, because people were doing hacks to get super? My point is that this is leading (and other features like await/defer will do this as well) to different design patterns. Such as Batman.js taking use of your extends keyword for consumers to implement their framework.
- jashkenas 15y agoWhat you're calling "class inheritance" and what other folks call "the prototype chain" are one and the same thing. It's not built-in to CoffeeScript ... it's fundamentally built-in to JavaScript. CoffeeScript is just making it three words: A extends B ... instead of the usual JavaScript hoop jumping: function ctor(){ this.constructor = A; } ctor.prototype = B.prototype; A.prototype = new ctor;
- MatthewPhillips 15y agoPrototypal inheritance and class inheritance are not one and the same. How do I call super or base in JavaScript? You know this, you had to add it to CoffeeScript. Do you not think the ability to call super functions leads to significantly different design patterns? If not, why did you add it? And why is EmcaScript.next probably going to have it?
- jashkenas 15y agoBuddy, we're going around in circles here ... Yes, JavaScript lacks a way to easily call "super", but that doesn't mean that the concept doesn't exist. Calling "super" means calling the immediate parent's version of the same function. CoffeeScript is just making it one word: super ... instead of the usual JavaScript prototype hoop jumping: Parent.prototype.method.apply(this, arguments);
- MatthewPhillips 15y agoWe're going in circles because I'm talking about design patterns and you're talking about the ability to add features into JS that weren't in mind when it was designed; it can be a lisp if that's what you want. I'll gladly defer to you on what JS is ultimately capable of, it was never the point of my post. Rather my point is that the different syntax is leading to a divergence libraries and frameworks; Batman.js is a great example of a framework that, while you can consume it from JS, you probably wouldn't want to. Thanks for the fun back and forth.
- jacobolus 15y agoDude, just admit you’re wrong about what kinds of patterns are natural or common in JS and about what was “in mind” when it was created. Here are some popular big JavaScript toolkits/frameworks: Closure Library, YUI, Dojo, Prototype, MooTools, ExtJS, Base2, JavaScriptMVC, Backbone. http://closure-library.googlecode.com/svn/docs/closure_goog_base.js.html http://closure-library.googlecode.com/svn/docs/closure_goog_... http://yuilibrary.com/yui/docs/api/classes/Base.html http://yuilibrary.com/yui/docs/api/classes/Base.html http://livedocs.dojotoolkit.org/dojo/declare http://livedocs.dojotoolkit.org/dojo/declare http://api.prototypejs.org/language/Class/ http://api.prototypejs.org/language/Class/ http://mootools.net/docs/core/Class/Class http://mootools.net/docs/core/Class/Class http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Base http://docs.sencha.com/ext-js/4-0/#!/api/Ext.Base http://base2.googlecode.com/svn/version/1.0.2/doc/base2.html#/doc/!base2.Base http://base2.googlecode.com/svn/version/1.0.2/doc/base2.html... http://javascriptmvc.com/docs.html#!jQuery.Class http://javascriptmvc.com/docs.html#!jQuery.Class http://documentcloud.github.com/backbone/docs/backbone.html#section-135 http://documentcloud.github.com/backbone/docs/backbone.html#... * * * And here are some other helpful resources: http://www.aminutewithbrendan.com/pages/20110216 http://www.aminutewithbrendan.com/pages/20110216 http://www.quora.com/How-was-classical-inheritance-*supposed*-to-be-done-in-ECMAScript-3 http://www.quora.com/How-was-classical-inheritance-*supposed... http://www.2ality.com/2011/06/prototypes-as-classes.html http://www.2ality.com/2011/06/prototypes-as-classes.html http://ejohn.org/blog/simple-javascript-inheritance/ http://ejohn.org/blog/simple-javascript-inheritance/ http://my.safaribooksonline.com/book/programming/javascript/9781449399115/code-reuse-patterns/klass http://my.safaribooksonline.com/book/programming/javascript/... http://javascript.crockford.com/prototypal.html http://javascript.crockford.com/prototypal.html http://bolinfest.com/javascript/inheritance.php http://bolinfest.com/javascript/inheritance.php http://dmitrysoshnikov.com/ecmascript/chapter-7-2-oop-ecmascript-implementation/ http://dmitrysoshnikov.com/ecmascript/chapter-7-2-oop-ecmasc...