创建和使用对象有很多方法。那么为什么/何时最好创建一个构造函数而不是声明类并使用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;
}
}