“标识符已被声明(...)”。如何在Chrome Devtools控制台中取消设置类变量? [英] "Identifier [...] has already been declared(…)". How to unset class variable in Chrome Devtools console?

查看:303
本文介绍了“标识符已被声明(...)”。如何在Chrome Devtools控制台中取消设置类变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chrome控制台中:

In Chrome console:

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

简而言之,如果没有页面重新载入。是否有任何方法可以在没有页面重新加载的情况下删除/清除/取消设置?

And simply no chances to unset a variable without page reload. Are there any means to delete/clear/unset it without page reload?

推荐答案

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

I look for this matter too. But can't found some useful on the web.

所以使用了下一个workarround:声明与class同名的变量。像这样:

So used next workarround: declare variable with same name as class. Like this:

var A = class A { constructor(x) { this.x = x } }

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

通过这种方式,它变得简单:

That's way it redifined easy:

var A = class A { constructor(x, y) { this.x = x; this.y = y } }
new A(2,3)
> A {x: 2, y: 3}

即使我们使用另一个变量,类型'A'

Even we use another variable, we still get objects with type 'A'

var AZ = class A {  constructor(x, y) { this.x = x; this.y = y } }
new AZ(2,3)
> A {x: 2, y: 3}

但是使用类名的类定义不会创建:

But class definition with used class name not creates:

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

这篇关于“标识符已被声明(...)”。如何在Chrome Devtools控制台中取消设置类变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆