My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Monday, March 24, 2008

My 5 cents about JavaScript Prototypal Inheritance

Few days ago I read an interesting post about Simple JavaScript Inheritance.
The most hilarious thing is that prototypal inheritance is truly simple, as John wrote, but there are still a lot of developers that do not probably understand perfectly them.

In this link you can find an "all in one" page about JavaScript prototypal inheritcance and classical emulation.

As I wrote at the end of that page, please do not hesitate to correct me if there is something wrong, or please ask me more details if there is something that is not so clear.

Sorry for my not perfect yet English, and have fun with JavaScript.

(happy easter too!)

5 comments:

  1. Hey!

    Thanks for the great articles! I've been following your blog for about 3 weeks now and learning a whole lot of great JavaScript programming patterns.

    Thanks a lot!

    ReplyDelete
  2. Hi friends,
    I implemented the inheritance in this manner.. Check this out...





    //Defination of top superclass
    function topSuperClass () {

    }
    //static members
    topSuperClass.staticProperty = function () {
    //static private members
    var TSCstaticPrivateVariable = "I am static private variable of top super class";
    var TSCstaticPrivateMethod = function () {
    return "I am a static private method of top super class";
    };
    return {
    //static public members
    TSCstaticPublicVariable : "I am static public variable of top super class",
    TSCstaticPublicMethod : function (){
    return "I am a static public method of top super class";
    }
    };
    }();


    //non static members
    topSuperClass.prototype.TSCnonStaticProperty = function () {
    //non static private members
    var TSCnonStaticPrivateVariable = "I am non static private variable of top super class";
    var TSCnonStaticPrivateMethod = function () {
    return "I am a non static private method of top super class";
    };
    return {
    //non static public members
    TSCnonStaticPublicVariable : "I am non static public variable of top super class",
    TSCnonStaticPublicMethod : function (){
    return "I am a non static public method of top super class";
    }
    };
    }();

    //Defination of superclass
    function superClass () {
    }
    //inheritance declaration
    superClass.prototype = new topSuperClass();

    //static members
    superClass.staticProperty = function () {
    //static private members
    var SCstaticPrivateVariable = "I am static private variable of super class";
    var SCstaticPrivateMethod = function () {
    return "I am a static private method of super class";
    };
    return {
    //static public members
    SCstaticPublicVariable : "I am static public variable of super class",
    SCstaticPublicMethod : function (){
    return "I am a static public method of super class";
    }
    };
    }();

    //non static members
    superClass.prototype.SCnonStaticProperty = function () {
    //non static private members
    var SCnonStaticPrivateVariable = "I am non static private variable of super class";
    var SCnonStaticPrivateMethod = function () {
    return "I am a non static private method of super class";
    };
    return {
    //non static public members
    SCnonStaticPublicVariable : "I am non static public variable of super class",
    SCnonStaticPublicMethod : function (){
    return "I am a non static public method of super class";
    }
    };
    }();


    //Defination of subclass
    function subClass () {
    }
    //inheritance Declaration
    //subClass.prototype = new topSuperClass();
    subClass.prototype = new superClass();


    //static members
    subClass.staticProperty = function () {
    //static private members
    var SBCstaticPrivateVariable = "I am static private variable of sub class";
    var SBCstaticPrivateMethod = function () {
    return "I am a static private method of sub class";
    };
    return {
    //static public members
    SBCstaticPublicVariable : "I am static public variable of sub class",
    SBCstaticPublicMethod : function (){
    return "I am a static public method of sub class";
    }
    };
    }();

    //non static members
    subClass.prototype.SBCnonStaticProperty = function () {
    //non static private members
    var SBCnonStaticPrivateVariable = "I am non static private variable of sub class";
    var SBCnonStaticprivateMethod = function () {
    return "I am a non static private method of sub class";
    };
    return {
    //non static public members
    SBCnonStaticPublicVariable : "I am non static public variable of sub class",
    SBCnonStaticPublicMethod : function (){
    return "I am a non static public method of sub class";
    }
    };
    }();

    //Inheritence Defination
    //superClass.prototype = new topSuperClass();
    //subClass.prototype = new superClass();

    var test = function (){
    var subClassObj = new subClass();
    alert(subClassObj.SBCnonStaticProperty.SBCnonStaticPublicMethod());
    alert(subClass.staticProperty.SBCstaticPublicMethod());

    alert(subClassObj.SCnonStaticProperty.SCnonStaticPublicMethod());
    alert(superClass.staticProperty.SCstaticPublicMethod());

    alert(subClassObj.TSCnonStaticProperty.TSCnonStaticPublicMethod());
    alert(topSuperClass.staticProperty.TSCstaticPublicMethod());


    }


    Thanks & Regards,
    Satish
    satish@analytica-india.com

    ReplyDelete
  3. That's funny. The same article from John Resig is what inspired me to write about prototype inheritance in javascript also.

    I even wrote a little framework called 'Proto' to make it more intuitive. I would love it if you would check it out and tell me what you think.

    http://devblog.techhead.biz/2009/04/prototype-based-programming-in_04.html

    ReplyDelete
  4. I needed to write you that little bit of remark to finally thank you the moment again over the beautiful concepts you have contributed on this page. It's really pretty open-handed with you to deliver easily just what a few individuals would have advertised as an e-book in making some cash for their own end, precisely now that you could possibly have done it in case you considered necessary. The tricks also acted to be a easy way to fully grasp that most people have the same eagerness just like mine to know more and more in respect of this issue. I'm certain there are thousands of more pleasant times ahead for many who read carefully your website.

    ReplyDelete

Note: Only a member of this blog may post a comment.