首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >“标识符[...]已声明(…)”。如何在Chrome Devtools控制台中取消设置类变量?

“标识符[...]已声明(…)”。如何在Chrome Devtools控制台中取消设置类变量?
EN

Stack Overflow用户
提问于 2016-12-08 09:09:20
回答 1查看 3.4K关注 0票数 5

在Chrome控制台中:

代码语言:javascript
复制
# 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(…)

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

EN

回答 1

Stack Overflow用户

发布于 2017-06-22 16:57:52

我也在寻找这件事。但在网上找不到有用的东西。

因此,下一个变通办法是:使用与class相同的名称声明变量。如下所示:

代码语言:javascript
复制
var A = class A { constructor(x) { this.x = x } }

new A(2)
> A {x: 2}

这就是重新定义它的简单方式:

代码语言:javascript
复制
var A = class A { constructor(x, y) { this.x = x; this.y = y } }
new A(2,3)
> A {x: 2, y: 3}

即使我们使用另一个变量,我们仍然会得到类型为'A‘的对象

代码语言:javascript
复制
var AZ = class A {  constructor(x, y) { this.x = x; this.y = y } }
new AZ(2,3)
> A {x: 2, y: 3}

但我们不能按类名使用class,只能按变量使用:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41030120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档