The Artima Developer Community
Sponsored Link

Java Community News
Josh Davis Explains JavaScript's Prototype Objects

4 replies on 1 page. Most recent reply: Jun 18, 2007 10:38 PM by J. Christopher Bare

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Josh Davis Explains JavaScript's Prototype Objects Posted: Apr 30, 2007 11:15 AM
Reply to this message Reply
Summary
Instead of classes, JavaScript uses prototypes as the basic building blocks of its object model. In a recent blog post, Josh Davis explains how prototypes compare to classes, and shows how to use the basic object-oriented techniques of inheritance and polymorphism in a prototypes-based language.
Advertisement

JavaScript is not only at the heart of popular Ajax frameworks, but is also one of the scripting languages that work well with Java on the server, and the next-genereration JavaScript is the foundation of ActionScript, the language behind Adobe's Flex framework. While JavaScript is an object-oriented language, instead of classes, it uses the concept of a prototype as the basic building-block of its object-model.

In a recent blog post, Prototype & Object Oriented JavaScript, Josh Davis explains that:

Prototype is a built-in JavaScript object which is a child of all objects and is used to create properties and methods for all instances of an object. You can use it on your JavaScript objects to create classes with externally defined methods...

You can also add methods to built-in JavaScript objects... You can also use prototype to create a class which extends another...

Davis shows several code examples of how to implement polymorphism and inheritance with prototypes.

What do you think of the prototype-based approach to OO, especially when compared with the class-based approaches used in Java, C++, or Ruby?


Leo Lipelis

Posts: 111
Nickname: aeoo
Registered: Apr, 2006

Re: Josh Davis Explains JavaScript's Prototype Objects Posted: Apr 30, 2007 2:24 PM
Reply to this message Reply
I like the semantics of it, since it gives you more control over object behaviors, especially at runtime. But I don't like Javascript's verbose and ugly syntax for it.

I like the idea that you can take any object and use it as a cookie-cutter to make more of it. It seems a lot more generic than making an arbitrary distinction between classes and objects.

Roger Voss

Posts: 27
Nickname: rogerv
Registered: Aug, 2005

Re: Josh Davis Explains JavaScript's Prototype Objects Posted: May 3, 2007 7:05 PM
Reply to this message Reply
Let's face it - classes and inheritance the JavaScript "hack" way really, really sucks. Only time is of any actual advantage (vs normal OOP languages) is to do AOP-like things such as adding methods to existing class at runtime, etc.

Is there a time and place to be able to compose classes at runtime? Absolutely, but a decent language shouldn't have dynamic composition of a class as the only way to achieve such. And the hack to pull off inheritance in JavaScript is an utter joke. I mean, pleeeeease - people have derided Perl for years for its whacky lego block assembly model of doing OOP. To do OOP in JavaScript, we're talking the same kind of whacko nonsense.

Course even JavaScript aficionados actually concede this - witness how ActionScript3 has classes and inheritance first class ala Java style. Case closed.

Leo Lipelis

Posts: 111
Nickname: aeoo
Registered: Apr, 2006

Re: Josh Davis Explains JavaScript's Prototype Objects Posted: May 4, 2007 2:05 PM
Reply to this message Reply
I like prototype inheritance model. I think your complaint is really mostly about the syntax. And yes, the syntax is ugly.

Besides syntax, what advantage is there for classes vs. objects distinction?

And yes, clean AOP type stuff that's part of the language without the absolutely nasty a fugly stuff like AspectJ bolt-on hack is a big win in my book.

J. Christopher Bare

Posts: 4
Nickname: cbare
Registered: Dec, 2003

Re: Josh Davis Explains JavaScript's Prototype Objects Posted: Jun 18, 2007 10:38 PM
Reply to this message Reply
Both prototype and class based inheritence are forms of delegation. The fact that you can emulate class based inheritence using prototypes points to the greater generality of prototypes.

And for a dynamic language, prototypes might be a better fit. I starting to think that moving from a static to a dynamically typed language requires a shift in thinking akin to that required to move from procedural to OOP. Further, if you look at a lot of C++ code (in particular) you'll notice that a lot of people move to an OO language but never make the shift in thinking.

I hope the crummy syntax for prototypes in Javascript gets fixed in JS2, or whatever they're calling it. And, I hope people aren't turned off to a really powerful idea by the confusing resulting from that crummy syntax.

-chris

Flat View: This topic has 4 replies on 1 page
Topic: Gavin King: In Defence of the RDBMS Previous Topic   Next Topic Topic: Reliable, RESTful Delivery over HTTP

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use