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();
__proto__
property. – Harlene