locked
Prototype and method

    Question

  • I want to know about inheritance in JavaScript through prototypes and methods with an (clear understandable) example?

    I want to know its all possible scenarios. (i know classical inheritance is not supported but still powerful)

    If someone could help , it would be really appreciated.

    Thanks


    maifs



    Thursday, January 10, 2013 10:49 AM

Answers

  • WinJS has Class methods for defining classes and Deriving methods for inheritance.

    There is a Quickstart document that references both methods here.

    Quick snippet:

    Create a class:

    var Quadruped= WinJS.Class.define(
    //constructor
        function (name) { this.name = name; },
    
        {
    //instance fields, properties
            name: 
    { get: function () { return this.name; } },
            walk:function(){ 
    /*do some walking*/;
    ;}
        },
    {
    //static fields, properties
        bloodTemperature:"Warm"
    }
        
    );

    Derive:

    var Dog = WinJS.Class.derive(Quadruped, // The constructor function. function (name) { this.name = name; }, // Additional instance members. { fleas: 0 }, // Additional static members. { legs: 4 });

    var myDog = new Dog("Scotty");

    Dog.walk(); Dog.fleas=10; console.log(Dog.bloodTemperature); console.log(Dog.name);


    • Edited by Ealsur Thursday, January 10, 2013 4:24 PM Typo
    • Marked as answer by Mehmood Ahmed Friday, January 11, 2013 7:25 AM
    Thursday, January 10, 2013 4:13 PM
  • This may not be what you were looking for, but I would strongly recommend the book JavaScript Patterns by Stoyan Stefanov.  He covers things like this in his book and I found it very helpful as a JavaScript newbie last year.
    • Marked as answer by Mehmood Ahmed Friday, January 11, 2013 7:25 AM
    Thursday, January 10, 2013 2:05 PM
  • Overriding can be achieved by defining a method with the same name when you call WinJS.Class.derive within the instance members declaration.

    Not really sure what other things you need, but you can also define Namespaces (like in c#) through WinJS.Namespace.define to group your Classes or objects and finally, you can "mix" 2 Classes with WinJS.Class.mix to create a Class that its the union of two given Classes.

    • Marked as answer by Mehmood Ahmed Friday, January 11, 2013 7:25 AM
    Thursday, January 10, 2013 7:36 PM
  • Hi Mehmood,

    As Ealsur has suggested, you can use those WinJS.xxx methods to help you define javascript class (or class inheritance) in Windows Store javascript app. This helps much for releasing you from handle those syntax details your self.

    If you want to get details about how the standard javascript (not WinJS specific) works with OOP programming, you can search for some online articles with "javascript  OOP" like keywords. Here are some ones I've found in web search engine:)

    #JavaScript and Object Oriented Programming (OOP)
    http://www.javascriptkit.com/javatutors/oopjs.shtml

    #Introduction to Object-Oriented JavaScript
    https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript?redirectlocale=en-US&redirectslug=Introduction_to_Object-Oriented_JavaScript

    #Object-Oriented Programming with JavaScript, Part I: Inheritance - Doc JavaScript
    http://www.webreference.com/js/column79/index.html

    BTW, you can also find some books on amazon.com talking about using javascript for Object-Oriented programming.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mehmood Ahmed Friday, January 11, 2013 7:25 AM
    Friday, January 11, 2013 3:45 AM
    Moderator

All replies

  • This may not be what you were looking for, but I would strongly recommend the book JavaScript Patterns by Stoyan Stefanov.  He covers things like this in his book and I found it very helpful as a JavaScript newbie last year.
    • Marked as answer by Mehmood Ahmed Friday, January 11, 2013 7:25 AM
    Thursday, January 10, 2013 2:05 PM
  • WinJS has Class methods for defining classes and Deriving methods for inheritance.

    There is a Quickstart document that references both methods here.

    Quick snippet:

    Create a class:

    var Quadruped= WinJS.Class.define(
    //constructor
        function (name) { this.name = name; },
    
        {
    //instance fields, properties
            name: 
    { get: function () { return this.name; } },
            walk:function(){ 
    /*do some walking*/;
    ;}
        },
    {
    //static fields, properties
        bloodTemperature:"Warm"
    }
        
    );

    Derive:

    var Dog = WinJS.Class.derive(Quadruped, // The constructor function. function (name) { this.name = name; }, // Additional instance members. { fleas: 0 }, // Additional static members. { legs: 4 });

    var myDog = new Dog("Scotty");

    Dog.walk(); Dog.fleas=10; console.log(Dog.bloodTemperature); console.log(Dog.name);


    • Edited by Ealsur Thursday, January 10, 2013 4:24 PM Typo
    • Marked as answer by Mehmood Ahmed Friday, January 11, 2013 7:25 AM
    Thursday, January 10, 2013 4:13 PM
  • Thanks Adam and Ealsur for your effort. Really appreciated and helpful.

    I 'll mark as Answer both of your post but right now , i need some more stuff  for example :  overdding  and other possible things.

    I want to get some more common details on it.


    maifs

    Thursday, January 10, 2013 6:24 PM
  • Overriding can be achieved by defining a method with the same name when you call WinJS.Class.derive within the instance members declaration.

    Not really sure what other things you need, but you can also define Namespaces (like in c#) through WinJS.Namespace.define to group your Classes or objects and finally, you can "mix" 2 Classes with WinJS.Class.mix to create a Class that its the union of two given Classes.

    • Marked as answer by Mehmood Ahmed Friday, January 11, 2013 7:25 AM
    Thursday, January 10, 2013 7:36 PM
  • Hi Mehmood,

    As Ealsur has suggested, you can use those WinJS.xxx methods to help you define javascript class (or class inheritance) in Windows Store javascript app. This helps much for releasing you from handle those syntax details your self.

    If you want to get details about how the standard javascript (not WinJS specific) works with OOP programming, you can search for some online articles with "javascript  OOP" like keywords. Here are some ones I've found in web search engine:)

    #JavaScript and Object Oriented Programming (OOP)
    http://www.javascriptkit.com/javatutors/oopjs.shtml

    #Introduction to Object-Oriented JavaScript
    https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript?redirectlocale=en-US&redirectslug=Introduction_to_Object-Oriented_JavaScript

    #Object-Oriented Programming with JavaScript, Part I: Inheritance - Doc JavaScript
    http://www.webreference.com/js/column79/index.html

    BTW, you can also find some books on amazon.com talking about using javascript for Object-Oriented programming.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Mehmood Ahmed Friday, January 11, 2013 7:25 AM
    Friday, January 11, 2013 3:45 AM
    Moderator