JS Coding Questions Logo
JS Coding Questions
#27💼 Interview💻 Code

What are classes in ES6

Advertisement

728x90

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,

javascript
1function Bike(model, color) {
2
3this.model = model;
4
5this.color = color;
6}
7
8Bike.prototype.getDetails = function () {
9
10return this.model + " bike has" + this.color + " color";
11};

Whereas ES6 classes can be defined as an alternative

javascript
1class Bike {
2
3constructor(color, model) {
4  this.color = color;
5  this.model = model;
6}
7
8getDetails() {
9  return this.model + " bike has" + this.color + " color";
10}
11}

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 29

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
27of476