Reputation: 1
From my understanding, a class is a blueprint with the following syntax:
class User {
constructor(name) {
this.name = name;
}
sayHi() {
alert(this.name);
}
}
let user = new User("John");
user.sayHi(); //John
user now has the properties of the class User, but function constructors, also do that:
function User(name) {
this.name = name;
this.isAdmin = false;
}
let user = new User("Jack");
alert(user.name); // Jack
alert(user.isAdmin); // false
I understand that classes have things like static, super, extend etc. How are they different?(Other than things like static etc)
Upvotes: 0
Views: 41
Reputation: 943216
How are Classes and Function Constructors different from one another
They aren't.
JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript. JavaScript classes provide "syntactic sugar" to create objects and deal with inheritance.
Upvotes: 3