在Chrome控制台中:
# One
class A {
constructor(x) { this.x = x }
}
class A {
constructor(x, y) { this.x = x; this.y = y }
}
VM602:1 Uncaught SyntaxError: Identifier 'A' has already been declared(…)
# Two
class A {
constructor(x) { this.x = x }
}
delete A
true
class A {
constructor(x) { this.x = x }
}
VM805:1 Uncaught SyntaxError: Identifier 'A' has already been declared(…)
# Three
A = null
null
class A {
constructor(x) { this.x = x }
}
VM817:1 Uncaught SyntaxError: Identifier 'A' has already been declared(…)
而且在不重新加载页面的情况下根本没有机会取消变量设置。有什么方法可以在不重新加载页面的情况下删除/清除/取消设置它吗?
发布于 2017-06-22 16:57:52
我也在寻找这件事。但在网上找不到有用的东西。
因此,下一个变通办法是:使用与class相同的名称声明变量。如下所示:
var A = class A { constructor(x) { this.x = x } }
new A(2)
> A {x: 2}
这就是重新定义它的简单方式:
var A = class A { constructor(x, y) { this.x = x; this.y = y } }
new A(2,3)
> A {x: 2, y: 3}
即使我们使用另一个变量,我们仍然会得到类型为'A‘的对象
var AZ = class A { constructor(x, y) { this.x = x; this.y = y } }
new AZ(2,3)
> A {x: 2, y: 3}
但我们不能按类名使用class,只能按变量使用:
var C = class B { constructor(x, y) { this.x = x; this.y = y } }
new C(2,3)
> B {x: 2, y: 3}
new B(2,3)
> VM342:1 Uncaught ReferenceError: B is not defined
https://stackoverflow.com/questions/41030120
复制相似问题