When / why use a class in JavaScript over a constructor?

Yes, there are many ways to create and use objects. So why / when is it better to create a constructor compared to a class declaration and use the constructor () method? My instructor said that it does not matter, but I do not believe him.

// 1
function Grumpy(name, profile, power){
    this.name = name;
    this.profile = profile;
    this.power = power;
}

Against

// 2
class Grumpy{
    constructor(name, profile, power){
        this.name = name;
        this.profile = profile;
        this.power = power;
    }
}
+4
source share
1 answer

JavaScript classes introduced in ECMAScript 2015 are primarily syntactic sugar over an existing JavaScript inheritance prototype. The class syntax does not introduce a new object-oriented inheritance model for JavaScript.

See MDN for more information.

+8
source

Source: https://habr.com/ru/post/1690782/


All Articles