gpt4 book ai didi

google-chrome - "Identifier [...] has already been declared(…)"。如何在 Chrome Devtools 控制台中取消设置类变量?

转载 作者:行者123 更新时间:2023-12-04 18:29:27 24 4
gpt4 key购买 nike

在 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(…)

而且根本没有机会在没有页面重新加载的情况下取消设置变量。有没有办法在不重新加载页面的情况下删除/清除/取消设置它?

最佳答案

我也在找这个问题。但是在网上找不到一些有用的。

所以使用下一个解决方法:声明与类同名的变量。像这样:

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}

但是我们不能通过类名使用类,只能通过变量:
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

关于google-chrome - "Identifier [...] has already been declared(…)"。如何在 Chrome Devtools 控制台中取消设置类变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com