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

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

 Uncaught SyntaxError: Unexpected token = 

我的代码,

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

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

您正在使用默认参数function,此function现在仅受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函数默认是有帮助的参数,然后纠正它们。