JS错误“重新声明”当 var 位于程序的第一行时?

发布于 2024-12-06 18:06:24 字数 178 浏览 1 评论 0原文

SCRPT5039:重新声明 const 属性第 1 行字符 1

line1:var editObj = null;

这是文件的开头,我检查以确保该变量不在任何文件中其他js文件被调用。是说我稍后再声明吗? (如果是这样,则行参考没有用)或者这有什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

不醒的梦 2024-12-13 18:06:24

我使用以下代码收到此错误:

var window;

我添加了此声明来解决使用以下代码时出现的节点错误:

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

没有上述声明,节点会抱怨。最后,我选择复制/粘贴,而不是尝试在节点和浏览器实现之间共享完全相同的文件。

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.

甜是你 2024-12-13 18:06:24

编辑:修复它。无论如何,对我来说。我在重新声明错误之前收到此错误:

HTML1113: Document mode restart from Quirks to IE9 Standards 

这表明 IE 发现它认为是错误,因此以 Quirks 模式再次加载页面。加载页面两次会使其认为所有内容都被声明了两次。所以解决办法就是找到IE不喜欢的东西。

首先,我通过在线 HTML 验证器运行该页面。接下来,我通过 jsLint 运行我的 JavaScript。经历了这一切之后,IE9 看起来很高兴。作为奖励,我有更好质量的代码。我希望。

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.

野生奥特曼 2024-12-13 18:06:24

我遇到了类似的问题和相同的错误,但我的第一行代码是 alert(0); 我插入它是为了确保脚本正在加载!有趣的是,脚本是根据 IE9 的开发工具加载的,但第一行从未执行,错误指向此 alert(0); 作为重新声明。我什至在它之前插入了行和空格,行数和字符数也相应地改变了。然而,这(显然)不是被重新声明的东西,因为它甚至不是声明,更不用说重新声明了!

我从脚本末尾删除了一些块,直到它执行 alert(0); (表明脚本已加载并已成功解析),我发现有问题的行是:

var screen;

原来是 IE9已经有一个 window.screen 与此声明发生冲突,并将我的 screen 重命名为 eScreen 解决了该问题。

所以我的答案是:不要相信 IE9 对重新声明位置的指示!

(还值得注意的是,该脚本以其原始形式在 IE7、IE8 和 IE10 上运行良好,只是在 IE9 上不行。 )

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.)

怎樣才叫好 2024-12-13 18:06:24

我的代码中遇到了同样的问题,结果是 IE 在重新声明出现时显示错误的行。就我而言,这是我稍后在代码中使用的历史记录。您应该检查整个代码以重新声明常量。您可以尝试注释掉部分代码,看看它何时抛出该错误。

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.

假装不在乎 2024-12-13 18:06:24

出现错误是因为您声明了一些与默认浏览器属性匹配的全局/局部变量。尝试

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

删除/注释此类声明并查看效果。

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.

秋意浓 2024-12-13 18:06:24

我也遇到了这个错误——问题是它触发的线路完全是假的。与OP一样,我的脚本中的早期行被标记为:

        var h_combinedView = true;

错误消息非常具有误导性:“0x800a041c - JavaScript运行时错误:Let/Const重新声明

标记的行不是const定义,其值在我的整个项目中定义一次,再也不会。

最终我将问题归结为实际重复的 const 定义。

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

(我已将在多个位置使用的定义移至共享文件中,但忘记删除一个副本)。错误消息是合法的——它是一个重复的常量定义——但是标记的行和标识符与问题无关。

没有什么比不准确的错误消息更能迫使我浏览我的代码了。

:-)

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.

:-)

七七 2024-12-13 18:06:24

我遇到了同样的错误并发现了这篇文章。虽然我没有与OP相同的根本问题,但我想我会分享我的解决方案,以防其他人犯我同样的错误并到达这里。我在一个单独的 js 文件中收到了错误。简化后,我发现我可以从以下代码生成错误:

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

该错误说它来自第一行。然而,foo 并没有被重新声明。问题是 localData 一定已在其他地方使用(不是在我的代码中)。无论 localData 在文件中声明的位置有多深,错误都会列为第一行代码。

因此,如果其他解决方案不起作用,请尝试重命名代码文件中的每个变量,以确定哪个变量可能导致问题。不要相信调试器指向正确的行。

遇到:HTML5/JS Window Store 应用程序。

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.

小耗子 2024-12-13 18:06:24

由于我执行 git merge 时出错,我发现我已将脚本包含在 HTML 中两次。这样,当它第二次运行脚本时,它重新声明了该变量。

它只给了我第一个 const 的错误,这让我感到困惑,当我尝试删除变量或向文件中添加另一个变量时,它总是给我第一行的错误。

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.

黄昏下泛黄的笔记 2024-12-13 18:06:24

如果您在浏览器中并且不小心声明了脚本源两次,您会看到这一点:

例如:

<script src="app.js" defer></script>
<script src="app.js" defer></script>

You'll see this if you're in browser and you accidentally declare the script source twice:

Eg:

<script src="app.js" defer></script>
<script src="app.js" defer></script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文