I have some questions related to prototypes.
If we have created one object say Dog
through the help of Person
function constructor.
Say Person
has 2 properties on each instance member.
var Dog = function (name,color){
this.name = name,
this.color = color
};
Now we have created 10 object from this like Pug
, Bulldog
, etc.
var Pug =new Dog('pug','white');
var Bulldog =new Dog('Bulldog','black');
Now suddenly we want to add one more property in Dog in such way that all object can access it. We can do it two ways:
- Adding it inside the function constructor
this.leg : 4
var Dog = function (name,color){
this.name = name,
this.color = color,
this.leg = 4
};
or
- Adding it using
Dog.prototype.leg = 4;
Now my question is in each of these variants, the property will be added its prototype property means as prototype member not as an instance member? why?
If after creating an object we want to add some property and we want it to be added as instance member means own property how can I do it?
What I am getting in browser console:
Pug { name : 'pug',color :'white'}
_ proto_ : { leg :4 }
What I am expecting:
Pug { name : 'pug',color :'white',leg :4 }
_proto_ :Object