我是JS类的新手,我主要做的是后端工作.
我正在玩新的JS类,所以我开始在这里看一些例子:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
我去了chrome(铬)开发人员工具控制台,我编写了Polygon类:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我想重新定义类,根据包含方法的示例,所以我写道:
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
get area() {
return this.calcArea();
}
calcArea() {
return this.height * this.width;
}
}
Run Code Online (Sandbox Code Playgroud)
这引发了一个错误: Uncaught SyntaxError: Identifier 'Polygon' has already been declared(…)
现在我明白ES6中有一个新的范围,并且类会自动使用新的范围等等......但实际上,我如何重新定义我的类?:d
我经常写Python,所以我习惯于重新定义我想要的东西.