Leetcode
Leetcode copied to clipboard
26. ES6 中的类是什么
在 ES6 中,Javascript 类主要是 JavaScript 现有的基于原型的继承的语法糖。例如,用函数表达式编写的基于原型的继承,如下所示,
function Bike(model, color) {
this.model = model;
this.color = color;
}
Bike.prototype.getDetails = function () {
return this.model + " bike has" + this.color + " color";
};
而 ES6 类可以定义为替代
class Bike {
constructor(color, model) {
this.color = color;
this.model = model;
}
getDetails() {
return this.model + " bike has" + this.color + " color";
}
}