javascript Internet Explorer 错误:“预期标识符”和“预期对象”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5158796/
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-10-25 16:11:35  来源:igfitidea点击:

Internet explorer errors: 'Expected identifier' and 'Object expected'

javascriptinternet-explorer

提问by aephixus

Hello I am getting this error in IE when i load any js file what should I do? Webpage error details

您好,当我加载任何 js 文件时,我在 IE 中遇到此错误,我该怎么办?网页错误详情

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET CLR 1.1.4322)
Timestamp: Tue, 1 Mar 2011 18:34:03 UTC


Message: Expected identifier
Line: 26
Char: 21
Code: 0
URI: http://localhost/learning/public/js/general.js


Message: Object expected
Line: 5
Char: 3
Code: 0
URI: http://localhost/learning/public/js/tests.js

回答by g.d.d.c

I'm willing to wager the cause is an extra comma at the end of an Object Declaration:

我敢打赌,原因是对象声明末尾有一个额外的逗号:

var valid = {
  'key': 'Value'
};

var invalid = {
  'key': 'Value',
};

Google Chrome / Firefox and I believe Safari are intelligent enough to ignore the extra comma. Internet Explorer sees the comma and expects another property to be assigned to the Object.

Google Chrome / Firefox 和我相信 Safari 足够智能,可以忽略额外的逗号。Internet Explorer 看到逗号并期望将另一个属性分配给对象。

回答by Tobias Sj?sten

It is likely either as @g.d.d.c says, a problem with a trailing comma, or the error comes from using reserved key words. When Explorer sees variable names like super, classand exportsit will strip those away before evaluating the code.

正如@gddc 所说,这可能是尾随逗号的问题,或者错误来自使用保留关键字。当资源管理器看见的变量名等superclass并且exports它将剥离评价的代码之前的那些远离。

class = 'asdf';

… becomes …

……变成……

= 'asdf';

And thus there is no identifier to assign the 'asdf'string to.

因此没有可将'asdf'字符串分配给的标识符。