javascript Uncaught SyntaxError: Unexpected token = in Google Chrome

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

Uncaught SyntaxError: Unexpected token = in Google Chrome

javascriptjquerygoogle-chromeconsolepnotify

提问by devo

I have a javascript function which accept an optional parameter. This works fine in Firefox, but in Google Chromeit shows:-

我有一个 javascript 函数,它接受一个可选参数。这在 中工作正常Firefox,但Google Chrome其中显示:-

Uncaught SyntaxError: Unexpected token =

My Code,

我的代码,

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

I have seen lot of similar questions but I can't realize my problem.

我看过很多类似的问题,但我无法意识到我的问题。

回答by Arun P Johny

You are using a default parameterfeature which is supported only by Firefox now.

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

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

回答by Mike Edwards

Javascript does not allow you to pass default arguments like that. You need to assign the default internally to the function.

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

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

回答by Shelvacu

Note: The other answers won't work if you have a boolean value that you want to default to true. That's because anything||truewill always return true. Instead you should do something like:

注意:如果您有一个想要默认为 的布尔值,其他答案将不起作用true。那是因为anything||true会一直返回true。相反,您应该执行以下操作:

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

回答by 123qwe

function [a-zA-Z0-9_]*?\s{0,}\([^=$)]*?(=)[^)$]*?\)it can be helpful to find js functions with default parameter and then correct them.

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