Search code examples
javascriptprototype

How can I attach prototype to Object Literals in JS


I am trying to understand JavaScripts Prototypal nature and I am trying to create object inheritance without using class like constructor functions.

How can I attach the prototype chain from Animals and then each to Cat and Dog if they are all three object literals?

Also, is there a way to do an Object.create and add more properties in literal form (like myCat).

I've added the code below & to paste bin http://jsbin.com/eqigox/edit#javascript

var Animal = {
    name        : null,
    hairColor   : null,
    legs        : 4,

    getName : function() {
        return this.name;
    },
    getColor : function() {
        console.log("The hair color is " + this.hairColor);
    }
};

/* Somehow Dog extends Animal */

var Dog = {
    bark : function() {
        console.log("Woof! My name is ");
    }
};

/* Somehow Cat Extends Animal */

var Cat = {
    getName : function() {
        return this.name;
    },

    meow : function() {
        console.log("Meow! My name is " + this.name);
    }
};


/* myDog extends Dog */

var myDog = Object.create(Dog);

/* Adding in with dot notation */
myDog.name = "Remi";
myDog.hairColor = "Brown";
myDog.fetch = function() {
    console.log("He runs and brings back it back");
};


/* This would be nice to add properties in litteral form */
var myCat = Object.create(Cat, {
    name        : "Fluffy",
    hairColor   : "white",
    legs : 3, //bad accident!
    chaseBall : function(){
        console.log("It chases a ball");
    }

});

myDog.getColor();

Solution

  • You can use Object.create to use your defined objects as prototypes. For instance:

    var Dog = Object.create(Animal, {
        bark : {
            value: function() {
                console.log("Woof! My name is " + this.name);
            }
        }
    });
    

    Now you can create a new Dog object:

    var myDog = Object.create(Dog);
    myDog.bark();  // 'Woof! My name is null'
    myDog.getColor();  // 'The hair color is null'
    

    Example: http://jsfiddle.net/5Q3W7/1/

    Alternatively, if you're working in the absence of Object.create, you can use constructors:

    function Animal() {
        this.name = null;
        this.hairColor = null;
        this.legs = 4;
    };
    
    Animal.prototype = {
        getName : function() {
            return this.name;
        },
        getColor : function() {
            console.log("The hair color is " + this.hairColor);
        }
    }
    
    function Dog() {
    }
    
    Dog.prototype = new Animal;
    Dog.prototype.bark = function() {
        console.log("Woof! My name is " + this.name);
    };
    

    Example: http://jsfiddle.net/5Q3W7/2/

    For more information about Object.create: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/create/

    For more information about Constructors and prototypes chains: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor

    https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_and_the_prototype_chain