开发者

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

开发者 https://www.devze.com 2023-04-08 17:16 出处:网络
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 variab开发者_高级运维le is not in any other

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 variab开发者_高级运维le 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?


I got this error with the following code:

var window;

I had added this declaration to workaround a node error when using the following code:

if (!window) {
  //node-specific stuff
}

Without the above declaration, node wold complain. In the end, I chose to copy/paste rather than try to share the exact same file between node and browser implementations.


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.


I had a similar problem with the same error, but my first line of code was an alert(0); which I had inserted to make sure the script was being loaded! Interestingly, the script was loaded according to IE9's developer tools, but the first line was never executed and the error pointed to this alert(0); as the redeclaration. I even inserted lines and spaces before it and the line and character number changed accordingly. However, this (obviously) wasn't the thing being redeclared because it's not even a declaration, let alone a redeclaration!

I deleted chunks from the end of the script until it executed the alert(0); (indicating that the script had loaded and been parsed successfully), and I discovered that the offending line was:

var screen;

Turns out IE9 already has a window.screen with which this declaration collided, and renaming my screen to eScreen fixed the problem.

So my answer is: don't trust IE9's indication of where the redeclaration is!

(It's also worth noting that the script ran fine in its original form on IE7, IE8 and IE10, just not on IE9.)


I had the same problem in my code and it turn out that IE show wrong line were the redeclaration appear. In my case it was history I use later in the code. You should check whole code for redeclaration of the constants. You can try to comment out part of the code and see when it throw that error.


The error comes because you have declared some global/local variable which matches with default browser property. Something like

var window = '';
var navigator = '';

Try removing/commenting such declaration and see the effects.


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.

:-)


I had the same error and came across this post. While I did not have the same root issue as the OP I thought I would share my solution in case others make my same mistake and arrive here. I received the error in a separate js file. After simplifying it, I found I could generate the error from the following code:

var foo = null;
var bar = null;
var localData = null;

The error said it was from the first line. However, foo was not being redeclared. The problem was that localData must have been used elsewhere (not in my code). No matter how far down in the file localData was declared, the error listed as being on the first real line of code.

So, if other solutions don't work, try renaming each variable in the code file to determine which one may be causing the problem. Don't trust that the debugger is pointing to the right line.

Encountered: HTML5/JS Window Store app.


From an error by me doing a git merge, I found that I had included the script in my HTML twice. This way, when it ran the script the second time, it redeclared the variable.

And it only gave me the error for the first const, which was confusing to me, as I tried to remove the variable or add another one to the file, it always gave me the error for the first line.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号