
JavaScript Prototypes Made Easy - toffeescript
http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/
======
ender7
Unfortunately, there's no real way to make JS prototypes easy, simply because
it's a poorly-designed system with way too many "wait, what?" moments.

If you want classical inheritance, there are many tiny JS libraries that will
give it to you. If you want classical prototypes, there are also JS libraries
that will give that to you. Or you can write your own.

But trying to make JS behave as either one by hand will just waste your time
and frustrate you.

(note: this is a good article for explaining the underlying system behind JS
prototypes, I just don't recommend that you interact with said system unless
you have to)

~~~
johncoltrane
I agree that's a good article. I would have loved to read it 12, 10 or 8 years
ago.

Coming from a design background, JavaScript was my first programming language
at the end of the century. I simply learned programming through JavaScript's
prototypal nature. I've used it extensively for many years before my first
experience with a more classical language (AS3…). JS prototypes seem very
natural to me. After a few years of AS3 (and a very short bit of C#), I'm not
sure at all that the "classical" way is fundamentally better than JS's way.

Newcomers to JavaScript should drop their "classical" baggage before they
embark. I'm starting to learn Python (2 week ends, now) and I'm not going to
bring my AS3isms and my JSisms with me. Is that too hard to learn a new
language with an open mind?

~~~
jimmytucson
I also came from a scripting background (Perl) but are you seriously telling
real programmers coming to Javascript from C, C++, and the like that they
should change their way of thinking to fit a truly broken system originally
used mostly by web designers?

~~~
tikhonj
Prototypes are not a "truly broken system originally used mostly by web
designers". The original idea came from a rather elegant language called
Self[1], not designed for web work at all. It really is a good idea
fundamentally, and, even if the JavaScript implementation is not great, it's
still very useful.

[1]:
[http://ranger.uta.edu/~nystrom/courses/cse3302-fa10/selfPowe...](http://ranger.uta.edu/~nystrom/courses/cse3302-fa10/selfPower.pdf)

Also, people coming from C do not have any preconceptions about OO--C does not
provide any OO facilities at all! They would have to change their way of
thinking when transiting to _any_ OO or functional language.

C++'s model is also not particularly strong. There is a famous quote from Alan
Kay (the initial inventor of OO): "I invented the term Object-Oriented and I
can tell you I did not have C++ in mind." I personally have not used C++ much,
but there seems to be a consensus that it's too complicated (especially in
regards to multiple inheritance) and too error-prone. So I think it's more
than reasonable to have people coming from C++ change their way of thinking!

Whenever you switch to a sufficiently different language, you have to change
your way of thinking. This is not only natural but also healthy.

Finally, implying that people using JavaScript aren't real programmers is
_extremely_ condescending.

------
iMark
Really nice explanation on a subject way too many JavaScript developers are
ignorant about. Definitely worth a read if you've any interest in the
language.

I agree with the comments that there's nothing inherently wrong with
prototypes (they can be tremendously useful and powerful, and make for a
refreshing change from the rigidity of classical inheritance in languages like
Java), but JavaScript's particular implementation, unfortunately, does a very
good job of disguising their utility.

------
mitjak
May I just compliment the author on the clever "dots" at the ends of the "J"
glyphs on the website logo that stay in place as you scroll.

~~~
mahcuz
What on earth are you talking about, man?

~~~
lukifer
I see it too, it seems like a CSS bug. The original commenter was probably
being sarcastic.

~~~
mitjak
I wasn't ahah. I genuinely thought it was an intentional design element.

------
jfaucett
great article that clearly articulates js prototypes. I'd have to disagree
with ender7 though, I think JS prototypes are not a "poorly-designed system"
they're just different from classical forms of OOP inheritance. And
contrarily, I would recommend anyone to delve deeper into prototypes - an
alternative method to class based OOP.

~~~
ufo
It still could be much better designed though, compareed to othe rprototipical
languages. Prototypes cannot be explicitly accessed or mutated, Object.create
wasn't even part of the standard and the whole syntax with function
constructors and having to set the prototype property is a very ugly hack.

~~~
jfaucett
I haven't used any other prototypical languages so I can't say how js
compares... Can you give me the best prototypical language? - I'd like to
check it out and see for myself. Still, as far as shimming up a clean cross
browser API prototyping is extremely usefull.

~~~
natrius
I don't know much about this, but I've heard Self mentioned as the
prototypical prototypical language many times.

<http://en.wikipedia.org/wiki/Self_(programming_language)>

~~~
tikhonj
It's the language where the idea came from originally. It's basically the
Smalltalk of prototype-based OO :).

Here's a nice paper about it:
[http://ranger.uta.edu/~nystrom/courses/cse3302-fa10/selfPowe...](http://ranger.uta.edu/~nystrom/courses/cse3302-fa10/selfPower.pdf)

------
sopooneo
If I am reading this right, then you can get efficiency gains by attaching
methods to an object's prototype rather than defining them directly in a
constructor. This way, the method is only created once, and is just reference
by all instances of whatever object.

If I am correct about all that, my point of confusion is simply that this
seems like a very awkward way to design things. Isn't it easier to read a
program if all of an object's properties and methods are defined together in
it's constructor, rather than spread out later as attachments to a prototype?

------
jsnk
I haven't come across cases where I was forced to use prototypes. Can someone
describe a situation where it would be necessary to use prototype? Or even
advisable to use prototype?

~~~
iMark
Part of the problem with JavaScript is that prototypes are an inherent part of
the language, but wrapped up behind syntactic sugar that hides them away on
the fringes of the language.

If you've created an object in JavaScript, you've used prototypes, just
indirectly.

If you want to use inheritance in Javascript prototypes are where it's at.

The ObjectName.prototype.fn = function() {} pattern is also a more memory
efficient way of defining object methods than doing function ObjectName()
{this.fn = function() {}}, as the method is only defined once in the former
case, but is defined every single time a object is created in the latter case.

Basically for the most part you don't have to use prototypes directly, but
more you know about them the better.

------
darkstalker
Looks similar to Lua's metatables with __index property

~~~
justincormack
Yes, Lua is a prototypal language too.

------
eragnew
marked 'to read this weekend'. looks very interesting.

------
lightblade
Cool! When do we get a NodeJS binding? XD

