Javascriptのプロトタイプの例


var Car = function (color, price){ this.distanceCovered = 0; this.color = color; this.price = price; this.gazorin = 0; return this; } Car.prototype.addGazorin = function (l){ this.gazorin += l; return this; } Car.prototype.run = function (){ var distance = 0; if (this.gazorin > 0){ while (this.gazorin){ this.distanceCovered ++; distance ++; this.gazorin --; } } return distance; } var car = new Car("red", 1000000).addGazorin(100); var distance = car.run(); // 100