“Identifier […] has already been declared(…)”. How to unset class variable in Chrome Devtools console?

自作多情 提交于 2020-01-03 15:59:45

问题


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?


回答1:


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

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}

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 we can't use class by class name, only by variable:

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



回答2:


I had the same issue where the error was complaining about Typescript class Name. Error - "Identifier classTest already been defined in the page" when refresh the page.

As per suggested by Alex, I assigned the variable to the class with same class name solved the issue.

var classTest = class classTest {

var classTest = class classTest {
   function addNumber(x: number, y: number){
       return x + y;
   }
}

var obj = new classTest();

$("body").one("loaded", () => {
   //Use obj to call the Class function
}



回答3:


It's fun to write/paste something into the console and then hit Enter to see what happens and then go back up to what was pasted in using the up arrow and change something a little bit then hit enter to re-run it. It works fine if 'var' is the keyword, but not if 'let' is used. This works fine:

var myNum = Math.floor(Math.random()* 110);

undefined

myNum
80 //use the arrow key to go up and change the number and it runs every time

This throws the error:

let num = Math.floor(Math.random()* 5);

VM838:1 Uncaught SyntaxError: Identifier 'num' has already been declared
    at <anonymous>:1:1 // If you use the arrow key to go up and change it.//

It's ok the first time but using the arrow key and going up to edit it and then re-run it that is the error I get. Same with const. I have tried 'Clear console' and 'Clear console history' but they don't work. So I guess when I play in the console I will limit myself to the keyword var.



来源:https://stackoverflow.com/questions/41030120/identifier-has-already-been-declared-how-to-unset-class-variable-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!