Box.color = "blue";
assigns a property to the function Box
, not to an instance of it. You can easily verify that by running console.dir(box)
and console.dir(Box)
.
If you want to assign the property to an instance, you have to create the instance first:
Box.prototype.color = "brown";
var box = new Box(null);
box.color = "blue"
or assign it inside the constructor function:
function Box(item) {
this.item = item;
this.color = 'blue';
}