makk_bit makk_bit - 3 months ago 6x
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

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;


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.