Constructor in JavaScript

Constructor in JavaScript

As in JavaScript there is no class keyword and the function serves as class definition, there is no need of defining constructor explicitly. The function defined for the class acts as constructor.

function Car(speed) {
    this.speed = speed;
    alert('car is initialised');
}

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

results matching ""

    No results matching ""