变量声明

1. 解构

要尽小心使用解构,当存在深层嵌套,加上重命名 ,默认值和类型注解时,将会变得难以理解

作用于函数参数

// 即类型是写在后面的, [number,number]
const input = [1,2]
function f([first,second]: [number,number]) {
    console.log(first)
    console.log(second)
}
f(input)

// 函数参数类型 {a:number}
const input = {a:1}
function f({a} : {a:number}) {
    console.log(a)
}
f(input)

2. 属性重命名

即此时冒号后面的 c 和 d 是属性重命名,而不是指明类型,如果想指明类型:

3. 默认值

4. 展开

但是,对象展开只包含对象的自身可枚举属性,因此当展开对象实例时,会丢失方法:

Last updated

Was this helpful?