function read_file(values) {values = merge({delete_after : "my default here"}, values || {});
// rest of code}
// simple implementation based on $.extend() from jQueryfunction merge() {var obj, name, copy,target = arguments[0] || {},i = 1,length = arguments.length;
for (; i < length; i++) {if ((obj = arguments[i]) != null) {for (name in obj) {copy = obj[name];
if (target === copy) {continue;}else if (copy !== undefined) {target[name] = copy;}}}}
return target;};
来使用
// will use the default delete_after valueread_file({ file: "my file" });
// will override default delete_after valueread_file({ file: "my file", delete_after: "my value" });
// the `= {}` below lets you call the function without any parametersfunction myFor({ start = 5, end = 1, step = -1 } = {}) { // (A)// Use the variables `start`, `end` and `step` here···}
// sample call using an objectmyFor({ start: 3, end: 0 });
// also OKmyFor();myFor({});
前ES2015,
有很多方法,但这是我的首选方法-它允许您传入任何您想要的内容,包括false或null。
function foo(a, b) {a = typeof a !== 'undefined' ? a : 42;b = typeof b !== 'undefined' ? b : 'default_b';...}
function myFunction(someValue = "This is DEFAULT!") {console.log("someValue --> ", someValue);}
myFunction("Not A default value") // calling the function without default valuemyFunction() // calling the function with default value
function myFunction(someValue) {someValue = (someValue === undefined) ? "This is DEFAULT!" : someValue;console.log("someValue --> ", someValue);}
myFunction("Not A default value") // calling the function without default valuemyFunction() // calling the function with default value
// OLD METHODfunction multiply(a, b) {b = (typeof b !== 'undefined') ? b : 1;return a * b;}
multiply(5, 2); // 10multiply(5, 1); // 5multiply(5); // 5
// NEW METHODfunction multiply(a, b = 1) {return a * b;}
multiply(5, 2); // 10multiply(5, 1); // 5multiply(5); // 5
不同的语法示例:
填充未定义与其他false sy值:
即使在调用时显式设置了该值,num参数的值也是默认值。
function test(num = 1) {console.log(typeof num);}
test(); // 'number' (num is set to 1)test(undefined); // 'number' (num is set to 1 too)
// test with other falsy values:test(''); // 'string' (num is set to '')test(null); // 'object' (num is set to null)
在通话时评估:
默认参数在调用时被评估,因此与其他一些语言不同,每次调用函数时都会创建一个新对象。
function append(value, array = []) {array.push(value);return array;}
append(1); //[1]append(2); //[2], not [1, 2]
// This even applies to functions and variablesfunction callSomething(thing = something()) {return thing;}
function something() {return 'sth';}
callSomething(); //sth
默认参数可用于以后的默认参数:
已经遇到的参数可用于以后的默认参数
function singularAutoPlural(singular, plural = singular + 's',rallyingCry = plural + ' ATTACK!!!') {return [singular, plural, rallyingCry];}
//["Gecko","Geckos", "Geckos ATTACK!!!"]singularAutoPlural('Gecko');
//["Fox","Foxes", "Foxes ATTACK!!!"]singularAutoPlural('Fox', 'Foxes');
//["Deer", "Deer", "Deer ... change."]singularAutoPlural('Deer', 'Deer', 'Deer peaceably and respectfully \ petition the government for positive change.')
function test(input_options){const options = {// defaultssomeKey: 'someDefaultValue',anotherKey: 'anotherDefaultValue',
// merge-in input options...input_options};
// from now on use options with no problem}