makk_bit makk_bit - 1 year ago 45
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;

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download