Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS error 'redeclaration' of var when it's first line in program?

SCRPT5039: Redeclaration of const property line 1 character 1

line1: var editObj = null;

This is the beginning of the file and I checked to make sure that variable is not in any other js files being called. Is it saying that I redeclare it later? (if so that line reference is not useful) or what is wrong with this?

like image 460
Damon Avatar asked Sep 28 '11 04:09

Damon


2 Answers

I was getting this error too -- problem is the line it fires on is completely bogus. Like the OP, it was an early line in my script that was being flagged:

        var h_combinedView = true;

The error message is very misleading: "0x800a041c - JavaScript runtime error: Let/Const redeclaration"

The flagged line is not a const definition, and the value on it is defined once in my entire project and never again.

Eventually I tracked the problem down to an actual duplicated const definition.

const ve = { Normal: 'default', Search: 'search', View: 'view', Alts: 'ViewAlts', Edit: 'edit' }

(I had moved a definition used in multiple places into a shared file & forgot to remove one copy). The error message was legit -- it was a duplicated const definition -- but the line and identifier flagged had NOTHING to do with the problem.

Nothing like inaccurate error messages to force me to walk through my code.

:-)

like image 183
boB Avatar answered Sep 23 '22 14:09

boB


EDIT: Fixed it. For me, anyway. I got this error before the redeclaration error:

HTML1113: Document mode restart from Quirks to IE9 Standards 

This suggests that IE finds what it thinks is an error, so loads the page again in Quirks mode. Loading the page twice makes it think everything is declared twice. So the solution is to find what IE didn't like.

First I ran the page through the online HTML validator. Next I ran my javascript through jsLint. After all that, IE9 seemed happy. And as a bonus I have better quality code. I hope.

like image 26
Chris Tolworthy Avatar answered Sep 21 '22 14:09

Chris Tolworthy