5 ms·
You can also use CoffeeScript to get more "traditional" OOP from JavaScript.
by thegoleffect 16y ago
You can also use CoffeeScript to get more "traditional" OOP from JavaScript.
- jimmydo 16y agoCoffeeScript does have really nice syntax for OOP, but I think JavaScript deserves some lovin' too. Well-written JavaScript (such as Backbone) looks pretty beautiful. Another plus is that new devs (if you have to worry about that sort of thing) don't have to learn a new language.
- krmmalik 16y agoYes, that would be me. I'm still getting my head round Javascript, cant take something new on at the moment. I havent even learned what inheritance is yet, so have no idea how Backbone.js would benefit me, even though im working on a project where i need to use a class.
- jashkenas 16y agoIn fact, they're both implemented in the same way, using the "standard" JavaScript pattern for setting up a prototype chain. (With a few minor extra features). Here's the annotated source code: http://documentcloud.github.com/backbone/docs/backbone.html#section-115 http://documentcloud.github.com/backbone/docs/backbone.html#...
- jimmydo 16y agoThat's really what I liked the most about the Backbone implementation. It stays very close to the standard JS pattern--essentially mirroring what someone would do if they wanted to setup the prototype chain and attach methods manually. It asks you to name the constructor method 'constructor' (when it could've easily been called something short like 'init' instead, for brevity). This corresponds well with the standard 'constructor' property available on each object, which points back to the constructor function that created the object. The '__super__' property is also a nice convenience, without doing something "fancy" like wrapping each method with logic that temporarily injects a 'super' property into the current instance for the duration of a method call. Although that's definitely clever and convenient, I prefer the simplicity of using '__super__'.
- jashkenas 16y agoInterestingly enough, the fancy approach for calling "super()" is actually prohibitively expensive ... making any method call in a subclass far slower than it otherwise could be. I don't think it's a viable option for a baseline library. http://www.broofa.com/2009/02/javascript-inheritance-performance/ http://www.broofa.com/2009/02/javascript-inheritance-perform...