// 默认参数代替短路 Falsy值,
// 如'',"",false,null,0,和NaN将不会被默认值替代:
function demo(text= '2022') {
console.log(text)
}
demo()
demo(333)
// 用动态键值对替换开关(即对象文字)
const UserRole = {
ADMIN: "Admin",
GENERAL_USER: "GeneralUser",
SUPER_ADMIN: "SuperAdmin",
};
function getRoute(userRole = "default role"){
switch(userRole){
case UserRole.ADMIN:
return "/admin"
case UserRole.GENERAL_USER:
return "/GENERAL_USER"
case UserRole.SUPER_ADMIN:
return "/superadmin"
default:
return "/"
}
}
console.log(getRoute(UserRole.ADMIN))
console.log(getRoute(UserRole.GENERAL_USER))
console.log(getRoute(UserRole.SUPER_ADMIN))
console.log(getRoute("Anything"))
console.log(getRoute())
console.log(getRoute(null))
// 用Object.assign设置默认对象
const someObject = {
title: null,
subTitle: "Subtitle",
buttonColor: null,
disabled: true
};
function creteOption(someObject) {
const newObject = Object.assign({
title: "Default Title",
subTitle: "Default Subtitle",
buttonColor: "blue",
disabled: true
},someObject)
return newObject
}
console.log(creteOption(someObject));
console