Thursday, October 16th, 2008

Another look at JavaScript inheritance

Category: JavaScript

Stoyan Stefanov of Yahoo! has published a nice article on JavaScript’s class-less objects.

This is published on JavaRanch, so it talks to the Java community, and uses that lense to explain the differences.

He delves into:

  • Constructor functions
  • Function objects and prototype property
  • Inheritance via the prototype
  • Inheritance by copying properties
  • Crockford’s beget object:
    javascript

    1. function begetObject(o) {
    2.   function F() {}
    3.   F.prototype = o;
    4.   return new F();
    5. }
  • YUI’s extend()

Posted by Dion Almaer at 12:44 pm
4 Comments

+++--
3.5 rating from 27 votes

4 Comments »

Comments feed TrackBack URI

A very nice explanation of the javascript way of doing things for folks being used to statically typed languages. Where was that article four years ago? It really would have made things easier for me. ;)

Comment by carrion — October 16, 2008

Thanks Carrion, you made my day ;)

Comment by stoyan — October 16, 2008

Nice and clear focus into javascript inheritance, thank you for sharing Stoyan.

Comment by fcaldera — October 17, 2008

As specified in one of my recent posts, the Douglas Crockford function does not need a new function for each object, since inheritance is via assigned prototype and not via new functions :-)

Object.create = function(Function){
// WebReflection Revision
return function(Object){
Function.prototype = Object;
return new Function;
}}(function(){});

In any case, nice article

Comment by WebReflection — October 24, 2008

Leave a comment

You must be logged in to post a comment.