Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 874
Green Avatar asked Dec 08 '16 01:12

Green


3 Answers

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
like image 163
Alex Maximenko Avatar answered Nov 01 '22 20:11

Alex Maximenko


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
}
like image 45
SpikeEdge Avatar answered Nov 01 '22 21:11

SpikeEdge


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.

like image 1
Ron Avatar answered Nov 01 '22 19:11

Ron