(function() { // First line of every script file
"use strict";
var _ = undefined; // For shorthand
// ...
aFunction(a, _, c);
// ...
})(); // Last line of every script
随着开发人员继续编写越来越健壮的 javascript 代码,与经典的 if (aParam) {...}相比,您调用的函数显式检查 undefined参数值的可能性越来越大。如果你继续使用 null与 undefined交替使用,你将站不住脚,因为它们都碰巧强制使用 false。
但是要注意,函数实际上可以告诉我们是否真的省略了一个参数(而不是设置为 undefined) :
f(undefined); // Second param omitted
function f(a, b) {
// Both a and b will evaluate to undefined when used in an expression
console.log(a); // undefined
console.log(b); // undefined
// But...
console.log("0" in arguments); // true
console.log("1" in arguments); // false
}
function myfunc(x=1,y=2,z=6){
console.log(x);
console.log(y);
console.log(z);
}
// skip y argument using: ...[,] and it's mean to undefine
// 1 argument for ...[,] 2 arguments for ...[,,] and so on.....
myfunc(7, ...[,], 4); //Output: 7 2 4