JavaScript ES6 Class
JavaScript classes, introduced in ES6, are primarily syntactical sugar over JavaScript’s existing prototype-based inheritance. the category syntax doesn't introduce a replacement object-oriented inheritance model to JavaScript. In early ES5 using function expression.
existing Prototype based inheritance:
function Bike(model,color) {
this.model = model;
this.color = color;
}
Bike.prototype.getInfo = function() {
return this.color + ' ' + this.model+ ' bike';
};
Defining classes:
Classes are actually “special functions”, and even as you'll define function expressions and performance declarations, the category syntax has two components: class expressions and sophistication declarations.
ES6 class
class Bike{
constructor(color, model) {
this.color= color;
this.model= model;
}
}
Benefits of Using class
Convenient, self-contained syntax.
A single, canonical thanks to emulate classes in JavaScript. before ES6, there have been several competing implementations in popular libraries.
More familiar to people from a class-based language background.
0 Comments