What are classes in ES6
In ES6, Javascript classes are primarily syntactic sugar over JavaScript’s existing prototype-based inheritance. For example, the prototype based inheritance written in function expression as below,
1 2 3 4 5 6 7 8 |
function Bike(model,color) { this.model = model; this.color = color; } Bike.prototype.getDetails = function() { return this.model + ' bike has' + this.color + ' color'; }; |
Whereas ES6 classes can be defined as an alternative
1 2 3 4 5 6 7 8 9 10 |
class Bike{ constructor(color, model) { this.color= color; this.model= model; } getDetails() { return this.model + ' bike has' + this.color + ' color'; } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.