Method in JavaScript

To define methods in a Class, all you have to do is just define a attribute and assign an function to it.

JavaScript
function Car() {
};
Car.prototype.speed= 'Car Speed';
Car.prototype.setSpeed = function(speed) {
    this.speed = speed;
    alert("Car speed changed");
};

var car1 = new Car(40);
var car2 = new Car(60);

car1.setSpeed(120);
car2.setSpeed(140);

results matching ""

    No results matching ""