125 karma · joined February 8, 2011
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.
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.
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__'.