Javascript 错误的可能情况:“预期的标识符、字符串或数字”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2149762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:58:02  来源:igfitidea点击:

Possible cases for Javascript error: "Expected identifier, string or number"

javascript

提问by psychotik

Some users are reporting occasional JS errors on my site. The error message says "Expected identifier, string or number" and the line number is 423725915, which is just an arbitrary number and changes for each report when this occurs. This mostly happens with IE7/ Mozilla 4.0 browsers.

有些用户偶尔会在我的网站上报告 JS 错误。错误消息显示“预期的标识符、字符串或数字”,行号为 423725915,这只是一个任意数字,发生这种情况时每个报告都会发生变化。这主要发生在 IE7/Mozilla 4.0 浏览器中。

I scanned my code a bunch of times and ran jslint but it didn't pick anything up - anyone know of the general type of JS problems that lead to this error message?

我扫描了我的代码很多次并运行了 jslint 但它没有找到任何东西 - 任何人都知道导致此错误消息的 JS 问题的一般类型吗?

回答by amercader

The cause of this type of error can often be a misplaced comma in an object or array definition:

此类错误的原因通常是对象或数组定义中的逗号错位:

var obj = {
   id: 23,
   name: "test",  <--
}

If it appears at a random line, maybe it's part of an object defintion you are creating dynamically.

如果它出现在随机行中,则它可能是您动态创建的对象定义的一部分。

回答by Roy Hyunjin Han

Using the word classas a key in a Javascript dictionary can also trigger the dreaded "Expected identifier, string or number" error because classis a reserved keyword in Internet Explorer.

使用单词class作为 Javascript 字典中的键也会触发可怕的“预期标识符、字符串或数字”错误,因为class是 Internet Explorer 中的保留关键字。

BAD

坏的

{ class : 'overlay'} // ERROR: Expected identifier, string or number

GOOD

好的

{'class': 'overlay'}

When using a reserved keyword as a key in a Javascript dictionary, enclose the key in quotes.

在 Javascript 字典中使用保留关键字作为键时,请将键括在引号中。

Hope this hint saves you a day of debugging hell.

希望这个提示可以为您节省一天的调试时间。

回答by cjstehno

Actually I got something like that on IE recently and it was related to JavaScript syntax "errors". I say error in quotes because it was fine everywhere but on IE. This was under IE6. The problem was related to JSON object creation and an extra comma, such as

实际上我最近在 IE 上得到了类似的东西,它与 JavaScript 语法“错误”有关。我在引号中说错误,因为它在任何地方都很好,但在 IE 上。这是在IE6下。该问题与 JSON 对象创建和额外的逗号有关,例如

{ one:1, two:2, three:3, }

IE6 really doesn't like that comma after 3. You might look for something like that, touchy little syntax formality issues.

IE6 真的不喜欢 3 之后的逗号。您可能会寻找类似的东西,敏感的小语法形式问题。

Yeah, I thought the multi-million line number in my 25 line JavaScript was interesting too.

是的,我认为我的 25 行 JavaScript 中的数百万行数也很有趣。

Good luck.

祝你好运。

回答by ErikE

This is a definitive un-answer: eliminating a tempting-but-wrong answer to help others navigate toward correct answers.

这是一个明确的非答案:消除一个诱人但错误的答案,以帮助其他人找到正确的答案。

It might seem like debugging would highlight the problem. However, the only browser the problem occurs in is IE, and in IE you can only debug code that was part of the original document. For dynamically added code, the debugger just shows the body element as the current instruction, and IE claims the error happened on a huge line number.

调试似乎会突出问题。但是,唯一出现问题的浏览器是 IE,在 IE 中您只能调试原始文档中的代码。对于动态添加的代码,调试器只是将 body 元素显示为当前指令,而 IE 声称错误发生在一个巨大的行号上。

Here's a sample web page that will demonstrate this problem in IE:

这是一个示例网页,将在 IE 中演示此问题:

<html>
<head>
<title>javascript debug test</title>
</head>
<body onload="attachScript();">
<script type="text/javascript">
function attachScript() {
   var s = document.createElement("script");
   s.setAttribute("type", "text/javascript");
   document.body.appendChild(s);
   s.text = "var a = document.getElementById('nonexistent'); alert(a.tagName);"
}
</script>
</body>

This yielded for me the following error:

这给我带来了以下错误:

Line: 54654408
Error: Object required

回答by Josue Alexander Ibarra

Just saw the bug in one of my applications, as a catch-all, remember to enclose the name of all javascript properties that are the same as keyword.

刚刚在我的一个应用程序中看到了这个错误,作为一个包罗万象的东西,请记住将所有与关键字相同的 javascript 属性的名称括起来。

Found this bug after attending to a bug where an object such as:

在解决了一个错误后发现了这个错误,其中一个对象,例如:

var x = { class: 'myClass', function: 'myFunction'};

generated the error (class and function are keywords) this was fixed by adding quotes

生成错误(类和函数是关键字)这是通过添加引号修复的

var x = { 'class': 'myClass', 'function': 'myFunction'};

I hope to save you some time

我希望能为你节省一些时间

回答by chopstik

http://closure-compiler.appspot.com/homewill pick this error up with an accurate reference to the actual line number in the offending script.

http://closure-compiler.appspot.com/home将通过准确引用违规脚本中的实际行号来找出此错误。

回答by B Seven

As noted previously, having an extra comma threw an error.

如前所述,有一个额外的逗号会引发错误。

Also in IE 7.0, not having a semicolon at the end of a line caused an error. It works fine in Safari and Chrome (with no errors in console).

同样在 IE 7.0 中,行尾没有分号会导致错误。它在 Safari 和 Chrome 中运行良好(控制台中没有错误)。

回答by Muhd

IE7 is much less forgiving than newer browsers, especially Chrome. I like to use JSLintto find these bugs. It will find these improperly placed commas, among other things. You will probably want to activate the option to ignore improper whitespace.

IE7 比新的浏览器,尤其是 Chrome 的宽容度要低得多。我喜欢使用JSLint来查找这些错误。它会发现这些放置不当的逗号等。您可能希望激活忽略不正确空格的选项。

In addition to improperly placed commas, at this blogin the comments someone reported:

除了不正确放置的逗号之外,在此博客的评论中有人报告了:

I've been hunting down an error that only said "Expected identifier" only in IE (7). My research led me to this page. After some frustration, it turned out that the problem that I used a reserved word as a function name ("switch"). THe error wasn't clear and it pointed to the wrong line number.

我一直在寻找一个错误,该错误仅在 IE (7) 中显示“预期标识符”。我的研究将我带到了这个页面。一番折腾后,原来是我使用保留字作为函数名(“switch”)的问题。错误不清楚,它指向错误的行号。

回答by king4test

Remove the unwanted , sign in the function. you will get the solution.

删除不需要的,登录功能。你会得到解决方案。

Refer this

参考这个

http://blog.favrik.com/2007/11/29/ie7-error-expected-identifier-string-or-number/

http://blog.favrik.com/2007/11/29/ie7-error-expected-identifier-string-or-number/

回答by Tousif Jamadar

This error occurs when we add or missed to remove a comma at the end of array or in function code. It is necessary to observe the entire code of a web page for such error.

当我们在数组末尾或函数代码中添加或错过删除逗号时,会发生此错误。对于此类错误,有必要观察网页的整个代码。

I got it in a Facebook app code while I was coding for a Facebook API.

我在为 Facebook API 编码时在 Facebook 应用程序代码中得到了它。

<div id='fb-root'>
    <script type='text/javascript' src='http://connect.facebook.net/en_US/all.js'</script>
    <script type='text/javascript'>
          window.fbAsyncInit = function() {
             FB.init({appId:'".$appid."', status: true, cookie: true, xfbml: true});            
             FB.Canvas.setSize({ width: 800 , height: 860 , }); 
                                                       // ^ extra comma here
          };
    </script>