Re:Simple JavaScript Inheritance with Backbone
by jpallen on Friday March 18, 2011 @05:13PM
(#2341995)
When I first started with Javascript I used to think it was an awful excuse for a programming language, with its weird prototypal inheritance and functions pretending to be classes. The more I get to know it though, the more impressed I am by how all these features can be used and built upon to create an easy and flexible language. It has been taken in so many directions and I think Backbone is a prime example of how it can be built upon well. I love coding using the Backbone framework but I like that it's not the only option for doing things in an object oriented way.
Re:Simple JavaScript Inheritance with Backbone
by city41 on Friday March 18, 2011 @07:10PM
(#2342397)
I've just started down my path of really learning JavaScript. My first stop was "JavaScript: The Good Parts", and the author there really advocates using closures to define objects and enable encapsulation. It seems in practice this approach isn't used often (such as Backbone here), why is that?
Re:Simple JavaScript Inheritance with Backbone
by munificent on Friday March 18, 2011 @09:54PM
(#2342739)
Performance. If you have n objects with m methods, that creates n * m function objects. With "normal" prototypal inheritance where those methods are on the prototype, it only requires n objects + m functions.
Re:Simple JavaScript Inheritance with Backbone
by jashkenas on Friday March 18, 2011 @10:14PM
(#2342775)
As always, munificent is right on the money.
The "closure pattern" is one of the most serious JavaScript anti-patterns -- it doesn't make much of a difference in tiny scripts, but for any decent-size JavaScript application, it can hurt performance terribly, especially regarding memory use.
For example, benchmarking the creation of 1 million objects, each with 10 member functions, in both the prototypal style and the closure style (in Chrome):
1 million prototypal objects: Brief pause, 32.4 MB
1 million closure objects: Minutes wait, 368.1 MB
If you're doing object-oriented code in JS, please use the "prototype" property, like the language is designed to do.
For more info, here's Brendan Eich on the subject:
Re:Simple JavaScript Inheritance with Backbone
by nxn on Saturday March 19, 2011 @02:25AM
(#2343099)
I don't even know if this can be considered the "closure" pattern anymore, but I wanted to try to get method sharing among the objects by doing something like this: https://gist.github.com/877259
The hope was that since the functions were created and bound within the function literal, the "create" method could simply assign references to the existing functions instead of creating a new function each time (ie, avoid what munificent mentioned).
Funny thing is, even though the last line indicates that the first and last objects' "ten" method is actually shared between them, I'm still getting about 370 MB of memory usage after creating 1 million objects. I am under assumption that the cause of this memory increase is the fact that each one of the million objects has its own local references to each of the 10 functions. When I put the 10 functions inside of the "create" method, memory usage goes up again to roughly 750 MB.
Re:Simple JavaScript Inheritance with Backbone
by jimmydo on Saturday March 19, 2011 @06:52PM
(#2344968)
That was a really interesting talk--thanks for the link!
Something that initially confused me was that Eich's use of the term "prototypal" (a pattern for implementing class-based inheritance) was different from Douglas Crockford's use of it (an alternative to class-based inheritance - http://javascript.crockford.com/prototypal.html).
In case anyone else finds this useful, here's how I sorted it out:
- "class-based" & "prototype-based" inheritance (Crockford refers to them as "classical" & "prototypal" inheritance). Different styles of instantiating objects.
- "closure pattern" & "prototypal pattern" (aka "prototypal inheritance" in this context). Different techniques for implementing class-based inheritance.
Re:Simple JavaScript Inheritance with Backbone
by jimmydo on Friday March 18, 2011 @09:11PM
(#2342661)
The mostly likely reason is probably familiarity. Most languages use the 'new' operator to create new objects, so it seems natural to follow the same pattern when creating a JS API that will likely be consumed by people who come from other languages.
The main advantage of Douglas Crockford's power constructor pattern is that it allows truly private variables or functions. I used to be adamant that this was the right way to go (and I still appreciate the pattern), but then I found that just following the convention of prefixing private members with an underscore worked fine--most people understand that it means "private". My code also started to look a little neater. All of my instance members are actually tied to the 'this' context object, rather being spread between nested functions or local variables declared within the constructors, and public/privileged methods declared on the new object itself.
Re:Simple JavaScript Inheritance with Backbone
by jashkenas on Friday March 18, 2011 @04:35PM
(#2341860)
In 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:
Re:Simple JavaScript Inheritance with Backbone
by jimmydo on Friday March 18, 2011 @05:43PM
(#2342107)
That'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__'.
Re:Simple JavaScript Inheritance with Backbone
by jashkenas on Friday March 18, 2011 @06:08PM
(#2342208)
Interestingly 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.
Re:Simple JavaScript Inheritance with Backbone
by jimmydo on Friday March 18, 2011 @04:04PM
(#2341734)
CoffeeScript 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.
Re:Simple JavaScript Inheritance with Backbone
by krmmalik on Friday March 18, 2011 @07:20PM
(#2342417)
Yes, 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.
The "closure pattern" is one of the most serious JavaScript anti-patterns -- it doesn't make much of a difference in tiny scripts, but for any decent-size JavaScript application, it can hurt performance terribly, especially regarding memory use.
For example, benchmarking the creation of 1 million objects, each with 10 member functions, in both the prototypal style and the closure style (in Chrome):
If you're doing object-oriented code in JS, please use the "prototype" property, like the language is designed to do.For more info, here's Brendan Eich on the subject:
http://www.aminutewithbrendan.com/pages/20110216
The hope was that since the functions were created and bound within the function literal, the "create" method could simply assign references to the existing functions instead of creating a new function each time (ie, avoid what munificent mentioned).
Funny thing is, even though the last line indicates that the first and last objects' "ten" method is actually shared between them, I'm still getting about 370 MB of memory usage after creating 1 million objects. I am under assumption that the cause of this memory increase is the fact that each one of the million objects has its own local references to each of the 10 functions. When I put the 10 functions inside of the "create" method, memory usage goes up again to roughly 750 MB.
Something that initially confused me was that Eich's use of the term "prototypal" (a pattern for implementing class-based inheritance) was different from Douglas Crockford's use of it (an alternative to class-based inheritance - http://javascript.crockford.com/prototypal.html).
In case anyone else finds this useful, here's how I sorted it out:
- "class-based" & "prototype-based" inheritance (Crockford refers to them as "classical" & "prototypal" inheritance). Different styles of instantiating objects.
- "closure pattern" & "prototypal pattern" (aka "prototypal inheritance" in this context). Different techniques for implementing class-based inheritance.
Underscore and Backbone will both teach you a lot. This chapter from Eloquent Javascript will also help motivate the rationale behind Underscore:
http://eloquentjavascript.net/chapter6.html
The main advantage of Douglas Crockford's power constructor pattern is that it allows truly private variables or functions. I used to be adamant that this was the right way to go (and I still appreciate the pattern), but then I found that just following the convention of prefixing private members with an underscore worked fine--most people understand that it means "private". My code also started to look a little neater. All of my instance members are actually tied to the 'this' context object, rather being spread between nested functions or local variables declared within the constructors, and public/privileged methods declared on the new object itself.
http://documentcloud.github.com/backbone/docs/backbone.html#...
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__'.
http://www.broofa.com/2009/02/javascript-inheritance-perform...
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.