makk_bit makk_bit - 4 months ago 7
Javascript Question

Does changing a constructor's property after its instances are created affect the instances? If yes, then how?

Suppose this is the constructor:

var Bike = function (speed) {
this.speed = speed;
};


These are the instances created:

var suzuki = new Bike(500);
var hayabusa = new Bike(550);
var honda = new Bike(400);


Now, what I want to do is to change the speed of all the bikes to
600
.

This can be done by changing the property of each object one by one:

suzuki.speed = 600;
hayabusa.speed = 600;
honda.speed = 600;


Can't it be changed by just changing the property of the class?

Bike.speed = 600;

Answer

new Bike(number).speed is instance variable.

Bike.speed would be 'class' (object) variable.

Those 2 are completely different and don't affect each other.

Comments