我有一个接受可选参数的javascript函数。这在Firefox
中运行良好,但在Google Chrome
中它显示:-
Uncaught SyntaxError: Unexpected token =
我的代码,
function errorNotification(text = "Something went wrong!") {
$.pnotify({
title: 'Error',
text: text,
type: 'error'
});
}
我已经看到了很多类似的问题,但我不能意识到我的问题。
发布于 2013-10-31 13:48:56
您正在使用的default parameter功能现在仅受Firefox支持。
function errorNotification(text) {
text = text || "Something went wrong!";
$.pnotify({
title: 'Error',
text: text,
type: 'error'
});
}
发布于 2013-10-31 13:49:47
Javascript不允许这样传递默认参数。您需要在内部将缺省值分配给函数。
function errorNotification(text) {
text || (text = "Something went wrong!");
$.pnotify({
title: 'Error',
text: text,
type: 'error'
});
}
发布于 2015-05-09 01:34:50
注意:如果您希望布尔值默认为true
,则其他答案将不起作用。这是因为anything||true
总是返回true
。相反,你应该这样做:
function foo(bar){
if(bar === undefined)
bar = true;
//rest of your function
}
https://stackoverflow.com/questions/19699257
复制相似问题