
No more than code.
ES6 中可以为函数的参数设置默认值,以此可以验证方法参数不能为空。
const isRequired = () => {
throw new Error('param is required')
}
const print = (num = isRequired()) => {
console.log(`printing ${num}`)
}
print(2) //printing 2
print() // error
print(null) //printing null
const object = { number: 10 }
// 不使用别名
const { number } = object
console.log(number) //10
// 使用别名
const { number: otherNumber } = object
console.log(otherNumber) //10