未捕获的SyntaxError:Google Chrome中的意外令牌=

dev*_*evo 16 javascript console jquery google-chrome pnotify

我有一个接受可选参数的javascript函数.这很好用Firefox,但在Google Chrome其中显示: -

Uncaught SyntaxError: Unexpected token =
Run Code Online (Sandbox Code Playgroud)

我的代码,

function errorNotification(text = "Something went wrong!") {
  $.pnotify({
      title: 'Error',
      text: text,
      type: 'error'
  });
}
Run Code Online (Sandbox Code Playgroud)

我见过很多类似的问题,但我无法实现我的问题.

Aru*_*hny 32

您正在使用默认参数功能,现在仅支持Firefox.

function errorNotification(text) {
    text = text || "Something went wrong!";
    $.pnotify({
        title: 'Error',
        text: text,
        type: 'error'
    });
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*rds 6

Javascript不允许您传递这样的默认参数.您需要在函数内部分配默认值.

function errorNotification(text) {
  text || (text = "Something went wrong!");
  $.pnotify({
      title: 'Error',
      text: text,
      type: 'error'
  });
}
Run Code Online (Sandbox Code Playgroud)

  • @devo默认参数值应该在[ECMAScript 6](http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts)最终确定时成为语言的一部分.Mozilla刚刚加入了对它的支持. (2认同)