javascript - Why is this prototype field taking precedence over the same object field? -
i'm in process of learning javascript prototypes , trying understand why happening. it's understanding when looking value of property, object checked before prototype. when then, print "brown"?
function box(item) { this.item = item; } box.prototype.color = "brown"; box.color = "blue"; var box = new box(null); console.log(box.color); >>> brown
box.color = "blue";
assigns property function box
, not instance of it. can verify running console.dir(box)
, console.dir(box)
.
if want assign property instance, have create instance first:
box.prototype.color = "brown"; var box = new box(null); box.color = "blue"
or assign inside constructor function:
function box(item) { this.item = item; this.color = 'blue'; }
Comments
Post a Comment