-1

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;
makkBit
  • 373
  • 2
  • 14
  • 4
    Your question asking if it can be done should be answered by testing it for yourself. After you've found that it *can't* be done that way, if your question is then "why", then it's because the language's design doesn't allow it, *thankfully*. –  Jul 17 '16 at 19:17
  • Actually I tested it myself. It didn't worked. I'm new to this. So I thought maybe there would be any other method to achieve this. That's why the question title at the end, states ' if yes, then how'. – makkBit Jul 17 '16 at 19:22
  • 1
    The answer is clearly *no*. A more insteresting question would be how to achieve something like that. See [Modifying all members of a class via an instance method](http://stackoverflow.com/q/30429536/1529630) – Oriol Jul 17 '16 at 19:58

2 Answers2

2

You can't do this. What you can do is change the prototype later. This will, however, only effect instances that don't have that property defined:

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

Bike.prototype.speed = 100;

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

console.log(suzuki.speed); //500
console.log(hayabusa.speed); //100
console.log(honda.speed); //100

Bike.prototype.speed = 200;

console.log(suzuki.speed); //500
console.log(hayabusa.speed); //200
console.log(honda.speed); //200

The only way to change all instances is actually to change all instances. But you could do something like saving the date when you set the property, and the date on the property of the prototype and define a getter that is doing what you want:

var Bike = function(speed){
  Object.defineProperty(this, 'speed', {
    get() {

      if(this.speedMoment && this.speedMoment > Bike.speedMoment) {
        return this._speed;
      } else {
        return Bike._speed;
      }
    },
    set(val) {
      Bike._speedMomentIter++;
      this.speedMoment = Bike._speedMomentIter;
      this._speed = val;
    }
  });

  if(speed) {
    this.speed = speed;
  }
};
Bike._speedMomentIter = 0;

Object.defineProperty(Bike, 'speed', {
  set(val) {
    Bike._speedMomentIter++;
    this.speedMoment = Bike._speedMomentIter;
    this._speed = val;
  }
});

Bike.speed = 100;

var suzuki = new Bike(500);
var hayabusa = new Bike(800);
var honda = new Bike();

console.log(suzuki.speed); //500
console.log(hayabusa.speed); //800
console.log(honda.speed); //100

Bike.speed = 200;

console.log(suzuki.speed); //200
console.log(hayabusa.speed); //200
console.log(honda.speed); //200
Lux
  • 17,835
  • 5
  • 43
  • 73
1

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.

Božo Stojković
  • 2,893
  • 1
  • 27
  • 52
  • Thanks! Can you also please tell the best solution for the above problem? Should I go with new Bike(number).speed = 600; or there's some other optimum method? – makkBit Jul 17 '16 at 19:16
  • 1
    You should proceed how you were going. Using `var bike_name = new Bike( speed );` and setting each speed – Božo Stojković Jul 17 '16 at 19:17