我正在用codeigniter和javascript做一个项目。但当我想定义像
function function_name(price = 0) or function function name(name = '')
在javascript中,它在IE浏览器中显示错误,但在另一个浏览器中没有显示任何错误,项目运行得很好。那么,在定义函数时,我将如何在javascript中定义默认值,以便每个浏览器都支持?
RORO模式
function function_name(params)
{
if(!params.price)
params.price = 0;
}
呼叫
function_name({ price : 1 })
function_name({});
我希望下面的片段能更好地理解这个问题。
我希望这能解决问题。
function foo(val) {
if(val === undefined) {
val = false;
}
console.log("initial value of variable val =>", val)
}
//it can be often shortened to something like:
function bar(name) {
name = name || 'Bob';
console.log("initial value of variable name =>", name)
}
//The above is true for ECMAScript <= 5. and also if you use chrome <49 there is issue.
//ES6 has proposed Default parameters. So the above could be re written as
function zoo(status = 'Checked') {
console.log(" initial value of variable status =>", status)
}
foo();
bar();
zoo();