函数参数的默认值

function books(js="javascript",h5="html5"){
	return `${js} and ${h5}`
}

console.log(books());
console.log(books("ECMAScript","html4"));

在这里,我们定义了一个函数books,这个函数需要接收2个参数,并且我们给这个参数都设置了默认值,
我们调用函数books();不传任何值的话就用设置的默认值输出javascript and html5。
当我们给两个函数都传值的时候books(“ECMAScript”,“html4”)就会输出传参的值ECMAScript and html4。
如果我们就传了第一个值,结果又会是什么呢?

function books(js="javascript",h5="html5"){
	return `${js} and ${h5}`
}
console.log(books("ECMAScript"));

结果输出的是ECMAScript and html5
我们发现第一个参数就会取传参的值,第二个参数就会取默认的值。
既然如此,如果我们第一个值不传,只传第二个值会如何呢?
尴尬。。。貌似不会 ,不知道行不行,暂时我也不知道 ,等学到后面回头再来就看看

猜你喜欢

转载自blog.csdn.net/wadedt/article/details/88396600