Ladies and gentlemen, I present you two snippets of code and ask for your advice.
The first one is mine, the second one belongs to the coder by the nickname vipatron. This discussion started as my attempt to grasp the topic of inheritance: Use Inheritance So You Dont Repeat Yourself. How inheritance is declared?
Naturally, some new questions arose, some additional materials were read and now I’m stuck once again.
Initially I tried to create multi-level structure with three constructors descending one from another and finally one object created by the last constructor and inheriting all the properties of all the constructors and prototypes above.
Both code snippets have declaration of three classes, one object and a series of checks at the bottom to find out whether our bottom-most object has all the properties we wanted.
Code #2 has mainTask, blod and secondaryTask as own properties of the final object(tony) which is what I was intending to have, but if we add properties through prototypes (Code #1) the whole system doesn’t work as planned, so I have several questions:
-
How to create bottom-most object (tony) and pass all the values of the constructors above (species, name, breed)? (Example #1) (if it is possible, of course)
-
How to access all the properties that has been added through prototypes? I expected that prototypeProps will contain mainTask, blood, secondaryTask (Example #1)
-
How it is used in real world? Perhaps it will be more instructive to see a real world example with concrete use of this technique.
-
If we don’t use prototypes, everything works fine (Example #2), but as I understand, the use of prototypes is recommended for complex data structures, this makes program execution faster. Is there a way for mainTask, blood, secondaryTask to be displayed in prototypeProps by adding them through prototypes (Example #1)?
-
Which of these two approaches is used in real life?
And the final question is:
Is this something that can be done at all or I’m just doing some work that has no use anywhere else?
My expected result was:
console.log(ownProps) => species: canis lupus familiaris, name: dog, breed: scottishTerrier, likes: food
console.log(prototypeProps) => mainTask: survive and reproduce, blod: warm, secondaryTask: help human
Or just another way of containing all this information within the bottom-most object (tony)
I will appreciate your thoughts and hints on this issue.
Code #1
var Creature = function (species) {
this.species = species;
};
Creature.prototype = {
constructor: Creature,
mainTask: "Survive and reproduce",
};
var Mammal = function (name) {
Creature.call(this);
this.name = name;
};
Mammal.prototype = {
constructor: Mammal,
blood: "Warm"
};
Mammal.prototype = Object.create(Creature.prototype);
Mammal.prototype.constructor = Mammal;
var Dog = function (breed) {
Mammal.call(this);
this.breed = breed;
};
Dog.prototype = {
constructor: Dog,
secondaryTask: "Help human"
};
Dog.prototype = Object.create(Mammal.prototype);
Dog.prototype.constructor = Dog;
let tony = new Dog("ScottishTerrier");
tony.likes = "food";
let ownProps = [];
let prototypeProps = [];
for (let property in tony) {
if(tony.hasOwnProperty(property)) {
ownProps.push(property);
} else {
prototypeProps.push(property);
}
}
console.log(ownProps)
console.log(prototypeProps)
console.log(tony)
Code #2
function Creature () {
this.mainTask = "survive and reproduce";
}
function Mammal () {
Creature.call(this);
this.blood = "warm";
}
Mammal.prototype = Object.create(Creature.prototype);
Mammal.prototype.constructor = Mammal;
function Dog () {
Mammal.call(this);
this.secondaryTask = "help human";
}
Dog.prototype = Object.create(Mammal.prototype);
Dog.prototype.constructor = Dog;
let myDogRex = new Dog();
console.log(myDogRex);
console.log("myDogRex.mainTask: ", myDogRex.mainTask);
console.log("myDogRex.blood: ", myDogRex.blood);
console.log("myDogRex.secondaryTask:", myDogRex.secondaryTask);
let tony = new Dog("ScottishTerrier");
let ownProps = [];
let prototypeProps = [];
for (let property in tony) {
if(tony.hasOwnProperty(property)) {
ownProps.push(property);
} else {
prototypeProps.push(property);
}
}
console.log(ownProps)
console.log(prototypeProps)
console.log(tony)