object - Use of prototype in below example Javascript -
while going through concepts of prototype. saw below example stackoverflow - add new element existing object
var myobj = function(){ this.property = 'foo'; this.bar = function(){ } } myobj.prototype.objprop = true; var newobj = new myobj();
my question use of "myobj.prototype.objprop = true;
" above code snippet.
i beginner. referred other post similar this. couldn't make out.
any on appreciated. thanks.
prototype used having common values in similar objects. property objprop
available objects not belong anyone.
when myobj.objprop
, objprop
searched inside myobj
. if not found, property searched in __proto__
.
also note if define property of same name inside object, myobj.objprop
, add property in object , not override proto one
var myobj = function(){ this.property = 'foo'; this.bar = function(){ } } myobj.prototype.objprop = true; var newobj = new myobj(); console.log(newobj); console.log(newobj.objprop);
Comments
Post a Comment