ECMAScript6: 默认参数

来自CloudWiki
跳转至: 导航搜索

ES6中,可以直接在参数列表中为形参制定默认值。

function makeRedirect(url = "/home", timeout = 2000, callback)
{
	console.log(url);
	console.log(timeout);
	console.log(callback);
}

//使用url和timeout的默认值
makeRedirect();

//使用url和timeout的默认值
makeRedirect(undefined, undefined, function(){});

//使用timeout的默认值
makeRedirect("/login");

//不是用timeout的默认值
makeRedirect("/login", null, function(){});