Javascript函数和默认参数,不适用于IE和Chrome [英] Javascript Functions and default parameters, not working in IE and Chrome
本文介绍了Javascript函数和默认参数,不适用于IE和Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个像这样的函数:
I created a function like this:
function saveItem(andClose = false) {
}
在Firefox中运行正常
It works fine in Firefox
在IE中,它会在控制台上显示此错误:
Expected ')'
In IE it gives this error on the console:
Expected ')'
在Chrome中,它会在控制台中显示以下错误:
Uncaught SyntaxError: Unexpected token =
In Chrome it gives this error in the console:
Uncaught SyntaxError: Unexpected token =
两个浏览器都将错误源标记为函数创建行.
Both browsers mark the source of the error as the function creation line.
推荐答案
您不能执行此操作,但可以执行以下操作:
You can't do this, but you can instead do something like:
function saveItem(andClose) {
if(andClose === undefined) {
andClose = false;
}
}
通常将其简化为:
function setName(name) {
name = name || 'Bob';
}
更新
Update
The above is true for ECMAScript <= 5. ES6 has proposed Default parameters. So the above could instead read:
function setName(name = 'Bob') {}
这篇关于Javascript函数和默认参数,不适用于IE和Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文