typeScript 数组 对象 解构 扩展

// 数据结构
let arra: number[] = [100, 200, 300, 400];
let arrb: Array<number> = [500, 600, 700, 800]

//  全结构
let [one, two, three, four] = arra;
//部分结构
let [o] = arrb;
let [, t] = arrb;
let [, , , f] = arrb;


console.log(one, two, three, four, '======123');
console.log(o, '第一位', t, '第二位', f, '第四位')

// 对象结构

//通用对象
let obja: any = {
    namea: 'zs',
    age: 18,
    sex: '女'
};

let { namea, age, sex } = obja;

console.log(namea, age, sex, '==========')

interface objb {
    nameb: string,
    ageb: number,
    sexb: string

}

let objb: objb = {
    nameb: 'ls',
    ageb: 19,
    sexb: 'nan'
}

let { nameb, sexb } = objb;

console.log(nameb, sexb, '=========')

//  结构对象重名

interface objc {
    namec: string,
    agec: number,
    sexc: string
}

let objc: objc = {
    namec: 'lis',
    agec: 19,
    sexc: '女'
}

let { namec: c1, agec: c2, sexc: c3 } = objc
console.log(c1, c2, c3, '==========123')

let c11: string;
let c22: number;
let c33: string;
({ c11, c22, c33 } = {
    c11: 'lis',
    c22: 19,
    c33: '女'
})
console.log(c11, c22, c33)

// 解构赋值
let [a, aa = 20] = [10];
console.log(a, aa)


// 解构赋值
let ja, jb
({ ja, jb } = { ja: 'aa', jb: undefined })
console.log(ja, jb, '============')

// 解构对象重赋值
let { one: _one = '哈哈', two: _two = 'xx' } = { one: '我是one' }
console.log(_one, _two, '====')

//结构用于函数参数
function x([first, second]: [number, number]): void {
    console.log(first);//1
    console.log(second);// 2
}
x([1, 2])

// 解构用于函数参树并设置默认值
function cc({ color, age = 33 }: { color: string, age?: number }): void {
    console.log('color的值:' + color, "age的值为:" + age)
}
cc({ color: 'aaa', age: 20 })
cc({ color: 'red' })

// 剩余模式
let [a1, b1, ...c] = [1, 2, 3, 4, 8]
console.log(a1, b1, c)

// 剩余模式用于解构
let oo = { texta: '我是a', textb: '我是b', textc: '我是c' }
let { texta, ...passthrough } = oo
console.log(texta, passthrough, '333333333333')

// 数组的展开操作
let onee: number[] = [1, 2]
let toww: number[] = [3, 4]
let threed: (number | string)[] = [0, ...onee, ...toww, 5]
console.log(onee, toww, threed)

// 对象展开
interface defaultobj {
    name: string,
    age: number,
    color: string
}
let defaultobj: defaultobj = {
    name: 'zs',
    age: 18,
    color: 'yellow'
}
interface targetboj1 {
    name: string,
    age: number,
    color: string,
    des: string
}
let targetobj: targetboj1 = {
    ...defaultobj,
    des: '描叙',
    name: 'xx'
}

// 函数
function addd(x: number, y: number): number {
    return x + y
}
let result = addd(2, 3)
console.log(result, addd(2, 3))

// 高阶函数 参数和返回值也是函数
let sume = (a: number, b: number, callback: (result: number) => void): void => {
    callback(a + b)
}

sume(100, 200, (result: number): void => {
    console.log('result=' + result)
})

// 命名空间

namespace wenDem {
    let a: string = '命名空间wenDemo中的变量a'
    // 接口
    interface PersonInterface {
        name: string,
        getInfo()
    }
    export class Person implements PersonInterface {
        name: string;
        constructor({ name }) {
            this.name = name
        }
        getInfo() {
            return "姓名:" + this.name
        }
    }
}

let pp = new wenDem.Person({ name: 'xxx' })
console.log(pp.getInfo(), '====123')




















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

web修理工

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值