I start with defining 2 independent constructors.
var Sword = function (attack,price){
this.attack = attack;
this.price = price;
};
var LegendarySword = function (){
this.instantKill = true;
};
These constructors create objects which are obviously not related to each other.
var sword = new Sword(1, 100);
console.log(sword);
//Object
attack: 1
price: 100
__proto__: Object
var superSword = new LegendarySword();
console.log(superSword);
//Object
instantKill: true
__proto__: Object
I now want all instances of "class" LegendarySword to have properties which are characteristic of Sword "class", i.e. attack and price values. This is because all legendary swords are swords in the end, but with some unique properties (like "instantKill").
So, after reading about prototypes and inheritance, I figured out I can do like this with a smile:
LegendarySword.prototype = new Sword();
The way I understand this line is that I am using Sword's constructor in addition to what LegendarySword already has when I am constructing new objects of LegendarySword class. I expect the LegendarySword objects to gain attack and price attributes, but at the same time I do not expect Sword objects to gain instantKill attribute.
I also expect that I will be able to provide arguments in LegendarySword constructor now:
var superSword2 = new LegendarySword (5, 1000);
I would expect this to take the arguments, just like Sword constructor does, but it obviously does not:
console.log(superSword2.attack); // results to undefined (which proves that it has them dug in somewhere in the __proto__, which is so indeed)
It looks like I can still get the problem resolved approaching it from a different end. If I somewhat, as I believe, unnecessarily complicate the constructors and add new methods to Sword and call those methods and pass arguments when creating a new Legendary Sword object:
var Sword = function (attack,price){
this.attack = attack;
this.price = price;
this.setAttack = function(attack){
this.attack = attack;
};
this.setPrice = function(price){
this.price = price;
};
};
var LegendarySword = function (attack, price){
this.instantKill = true;
this.setAttack(attack);
this.setPrice(price);
};
LegendarySword.prototype = new Sword();
var sword = new Sword(1, 100);
console.log(sword);// gives correct object attributes
var superSword = new LegendarySword(10,5000);
console.log(superSword); // gives correct object attributes
But this is really ugly and my question is why cannot Javascript figure out that new attributes need to be added when I am adjusting a prototype of a constructor? This would be so intuitive to me.
Is there may be an alternative way to solve the problem?