Javascript SCRIPT1028:需要的标识符、字符串或数字

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

SCRIPT1028: Expected identifier, string or number

javascriptinternet-explorer

提问by user1712040

I'm running a plugin that displays an events calendar. It works great in all browsers except in IE compatibility mode. When that option is checked, the calendar disappears. I believe its a JS error.

我正在运行一个显示事件日历的插件。它适用于所有浏览器,除了 IE 兼容模式。选中该选项后,日历就会消失。我相信它是一个 JS 错误。

IE Debugger Error:

IE 调试器错误:

element.qtip({
    content: {
    text: event.description,
    title: {
    text: 'Description',
    }
  },
position: {
    at: 'top right',
    adjust: {
    x: 0, y: 30
   },
},

In my plugin editor this is the code:

在我的插件编辑器中,这是代码:

element.qtip({
  content: {
  text: event.description,
  title: {
  text: '<?php _e('Description', 'event_espresso'); ?>',
  }
},
position: {
   at: 'top right',
   adjust: {
   x: 0, y: 30
  },
},

I'm not great at debugging so any help would be appreciated.

我不擅长调试,所以任何帮助将不胜感激。

If it helps, here is the page: http://www.mbausa.org/calendar/

如果有帮助,请访问以下页面:http: //www.mbausa.org/calendar/

回答by Bj?rn

Internet Explorer have troubles with trailing commas in objects and arrays;

Internet Explorer 遇到对象和数组中尾随逗号的问题;

title: {
    text: 'Description', //<--
}

You probably want:

你可能想要:

title: {
    text: 'Description'
}

回答by Mike McLin

There are 2 common causes for this error. Either having a trailing comma when inappropriate, or using a JavaScript reserved word. In your case, you have 2 unnecessary commas. Below is the correct code snippet, with comments where I removed the commas.

此错误有 2 个常见原因。在不合适时使用尾随逗号,或使用 JavaScript 保留字。在您的情况下,您有 2 个不必要的逗号。下面是正确的代码片段,其中包含我删除逗号的注释。

element.qtip({
  content: {
  text: event.description,
  title: {
    text: '<?php _e('Description', 'event_espresso'); ?>' // Removed Comma
  }
},
position: {
  at: 'top right',
  adjust: {
    x: 0, y: 30
  } // Removed Comma
},

I actually did a blog post (and video) explaining the error and showing examples and fixes. It can be found here: http://mikemclin.net/fixing-error-script1028-expected-identifier-string-or-number/

我实际上做了一篇博文(和视频)解释错误并展示示例和修复。可以在这里找到:http: //mikemclin.net/fixing-error-script1028-expected-identifier-string-or-number/

回答by Pierre

Old version of IE doesn't support mal-formated JSON String.

旧版本的 IE 不支持格式错误的 JSON 字符串。

You should never put a comma ',' separator when no braces '[', accolades '{' or new object properties come after.

当后面没有大括号“[”、赞誉“{”或新的对象属性时,您不应该使用逗号“,”分隔符。

Try :

尝试 :

position: {
at: 'top right',
adjust: {
   x: 0, y: 30
  } // <-- no comma here
},

instead of :

代替 :

position: {
at: 'top right',
adjust: {
   x: 0, y: 30
  }, // <-- comma here
},

回答by igasparetto

If you are using Vuex and the issue manifests at the computedhook calling mapState, then the issue is with the spread operator.

如果您正在使用 Vuex 并且问题出现在computedhook 调用处mapState,那么问题出在扩展运算符上。

},
computed: {
  ...mapState({

Use babel to fix it: https://babeljs.io/docs/en/babel-plugin-proposal-object-rest-spread

使用 babel 修复:https: //babeljs.io/docs/en/babel-plugin-proposal-object-rest-spread

回答by El Ronnoco

Rather than work round compatibility mode you can force non-compatibility mode with...

您可以强制使用不兼容模式,而不是使用兼容模式...

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />

in your <head>tag.

在你的<head>标签中。

回答by lulalala

Another possible error is due to the reserved keyword being used as hash key.

另一个可能的错误是由于保留关键字被用作散列键。

IE8 errors when defining a Javascript object?

定义 Javascript 对象时出现 IE8 错误?

When I use {class:'icon'}I would also get this error. Other IE8 keywords would probably do the same too.

当我使用时,{class:'icon'}我也会收到此错误。其他 IE8 关键字也可能会这样做。

回答by Rikard Askel?f

I got this error when trying to import a ES6 module without transpilation through Babel or similar tools.

我在尝试通过 Babel 或类似工具导入 ES6 模块而不进行转译时遇到此错误。

This row generated the error:

这一行产生了错误:

import myES6module from 'my-npm-ES6-module'

从“my-npm-ES6-module”导入 myES6module

The solution is to make sure your workflow actually transpiles it to browser-compatible JavaScript.

解决方案是确保您的工作流实际上将其转换为与浏览器兼容的 JavaScript。