是的,有多种创建和使用对象的方法。那么为什么/何时创建一个构造函数比声明一个类并使用Constructor()方法更好呢?我的教练说这没什么用,但我不相信他。
// 1
function Grumpy(name, profile, power){
this.name = name;
this.profile = profile;
this.power = power;
}
与
// 2
class Grumpy{
constructor(name, profile, power){
this.name = name;
this.profile = profile;
this.power = power;
}
}
ECMAScript 2015中引入的JavaScript类主要是对JavaScript现有的基于原型的继承的语法糖。类语法不会向JavaScript引入新的面向对象的继承模型。
有关更多失败的信息,请参见MDN
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句