繁体   English   中英

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

[英]Uncaught SyntaxError: Unexpected token = in Google Chrome

我有一个接受可选参数的javascript函数。 这在Firefox运行良好,但在Google Chrome显示: -

Uncaught SyntaxError: Unexpected token =

我的代码,

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

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

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

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

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

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

注意:如果您有一个默认为true的布尔值,则其他答案将不起作用。 那是因为anything||true都将永远返回true 相反,你应该做的事情如下:

function foo(bar){
  if(bar === undefined)
    bar = true;
  //rest of your function
}

function [a-zA-Z0-9_]*?\\s{0,}\\([^=$)]*?(=)[^)$]*?\\)找到js函数默认是有帮助的参数,然后纠正它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM