By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 844 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Why Prototype?

P: n/a
What is the reason why Javascript, unlike other languages, uses
prototype instead of classical inheritance?

I know you can do classical inheritance with a bit of work, but it
seems like the kind of thing that would be nice out of the box, and I
don't understand why prototype is preferable.

Tom Longson (nym)
http://igargoyle.com/

Jan 9 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
to*****@gmail.com wrote:
What is the reason why Javascript, unlike other languages,
uses prototype instead of classical inheritance?
That is incorrect. JavaScript, and ECMAScript implementations in general,
are not the only programming languages around that support prototype-based
inheritance.
I know you can do classical inheritance with a bit of work,
You can only implement certain features of that.
but it seems like the kind of thing that would be nice out
of the box,
That is probably what the creators of JavaScript 2.0, JScript 7.0 (.NET)
and ECMAScript 4 thought, too.
and I don't understand why prototype is preferable.


<URL:http://en.wikipedia.org/wiki/Prototype-based_programming> pretty
much covers the pros and cons. You should also search the archives at
groups.google.com before you ask questions here. We had this before.

<URL:http://jibbering.com/faq/>
PointedEars
Jan 9 '06 #2

P: n/a

<to*****@gmail.com> wrote:
What is the reason why Javascript, unlike other languages, uses
prototype instead of classical inheritance?
http://wp.netscape.com/comprod/colum...vators_be.html

the original reasons are

"[...] If they're writing small pieces of code, they just want to get their
code
done with the minimal amount of fuss. Finally, we agreed that this new
language
should look like Java, but be a scripting language."

" Content creation should not be recondite. It should not be this bizarre
arcana
that only experts and gold-plated computer science gurus can do.
There are economic advantages to lowering the entry costs in creating
web content and in sharing it or aggregating it [...]"

a prototype-based language allowed that


I know you can do classical inheritance with a bit of work, but it
seems like the kind of thing that would be nice out of the box, and I
don't understand why prototype is preferable.


you don't exactly do "classical inheritance" with JavaScript,
it look like it but in fact it is inheritance by delegation,
and imho it's not preferable or not, it's just a different way
of "thinking" or "doing".

What you can not do with prototype that you can do with class ?

zwetan



Jan 10 '06 #3

P: n/a
to*****@gmail.com wrote:
What is the reason why Javascript, unlike other languages, uses
prototype instead of classical inheritance?

I know you can do classical inheritance with a bit of work, but it
seems like the kind of thing that would be nice out of the box, and I
don't understand why prototype is preferable.

Tom Longson (nym)
http://igargoyle.com/


I believe that the prototypal pattern has more expressive power than the
classical pattern.

Recall that Java had a crack as being the preferred language for writing
interactive applications in the browser. It failed there where JavaScript is now
succeeding.

People who have been trained in the classical pattern are often confused when
first exposed to prototypal ism. Don't let that bother you. It is worth
learning. It will also make you a better classical programmer.

http://www.crockford.com/javascript
Jan 10 '06 #4

P: n/a
to*****@gmail.com wrote:
What is the reason why Javascript, unlike other languages, uses
prototype instead of classical inheritance?
Well, I'm a C++/c programmer at heart, as well as a reasonably
successfull javascript developer as well. The reason I mention that
is simply to provide some background into my perceptions.

As to specificly why the prototype approach was used over other methods
is something you'd have to address to the original developers, but
whatever their reason , it seems to work well enough for me.

One of the things I've noticed about javascript over time, is that
it functions very nicely as a versionless language.

If an array is is missing a member function like .pop() it does
not take much to detect same and prototype the missing function,
allowing your actual application code to function as if the Array.pop()
function had always been there.

This versionless ability is one of the things that really makes
javascript an interesting language to me.
I know you can do classical inheritance with a bit of work, but it
seems like the kind of thing that would be nice out of the box, and I
don't understand why prototype is preferable.


Again , I was not there when the team built the language, but
I can imagine that the versionless prototype concept was a big deal, and
that it's similarity to *classical* inheritance had a tendancy to
suppress the latter as being somewhat redundant.

I think it's something that grows on you as while keeping in mind
what you know from other languages , it seems to work best if
you give the language some room to mold your design process to
fit it's somewhat unique nature. It really is a nifty language,
and I give you that opinion against a backdrop of having developed
C++ since it's invention.

If you play with it for a while with an open mind, it really does
make a lot of sense, and while not the same as other languages
it is just as valid , and possibly more so in the context in
which it exists.

--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML, CSS,Javascript
--=<> Internet Programming since 1994 <>=-- DHTML NSAPI TCP/IP
--=<> http://resume.drclue.net <>=-- AJAX, SOAP, XML, HTTP
--=<> http://www.drclue.net <>=-- SERVLETS,TCP/IP, SQL
--.
Jan 11 '06 #5

P: n/a

"Dr Clue" wrote:
to*****@gmail.com wrote: [snip]
I know you can do classical inheritance with a bit of work, but it
seems like the kind of thing that would be nice out of the box, and I
don't understand why prototype is preferable.
Again , I was not there when the team built the language, but
I can imagine that the versionless prototype concept was a big deal, and
that it's similarity to *classical* inheritance had a tendancy to
suppress the latter as being somewhat redundant.


from
http://www.mozilla.org/js/language/
the "javascript at ten years" powerpoint have this phrase

"I hacked JS prototype in ~1 week in May"

and then on slide 5

" Design Goals
* Make it easy to copy/paste snippets of code
* Tolerate "minor" errors (missing semicolons)
* Simplified onclick, onmousedown, etc. event handling, inspired by
HyperCard
* Pick a few hard-working, powerful primitives
* First clas functions for procedural abstraction
* Objects everywhere, prototype-based
* Leave all elase out!
"

[snip]
If you play with it for a while with an open mind, it really does
make a lot of sense, and while not the same as other languages
it is just as valid , and possibly more so in the context in
which it exists.


and then slide 14

"
* Hackers discovered JS's FP and Self-ish OOP features
* And good hackers like those features
"

:)

zwetan
Jan 11 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.