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);
TypeScript
class Car {
    speed: string;
    setSpeed(speed: string) {
        this.speed = speed;
        alert("Car speed changed");
    }
}

let car1 = new Car();
let car2 = new Car();

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

results matching ""

    No results matching ""