ps: 读 speaking js
1 Named Parameters
这种使用 会非常多
selectEntries(step=2)
selectEntries(end=20, start=3)
selectEntries()
function selectEntries(options) {
options = options || {};
var start = options.start || 0;
var end = options.end || getDbLength();
var step = options.step || 1;
...
}
还有这种
selectEntries(posArg1, posArg2, { namedArg1: 7, namedArg2: true });
2 Optional Parameters
function bar(arg1, arg2, optional) {
if (optional === undefined) {
optional = 'default value';
}
if (!optional) {
optional = 'default value';
}
}
还可以这样
if (arguments.length < 3) {
optional = 'default value';
}
引用值
function incRef(numberRef) {
numberRef[0]++;
}
var n = [7];
incRef(n);
console.log(n[0]); // 8
Unexpected Optional Parameters
这种常用的 例子
[ 1, 2, 3 ].map(function (x) { return x * x })
[ '1', '2', '3' ].map(parseInt)
[ 1, NaN, NaN ]