TypeScript应用 极速上手(4)

本文介绍了如何创建一个JavaScript对象,包括属性定义、默认值设置,以及如何使用JSON.stringify()将对象转换为字符串并解析回新对象,最后探讨了对象深浅拷贝的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、对象类型

2、示例

const emp1 = {
    name : {
        first : '四',
        last : '张'
    },
    gender :  'male' as 'male' | 'femal'| 'other' | 'unknown', //指定初始值为male
    salary:8000,
    bouns:undefined as (number | undefined),
    performance:3.5,
    badges: ['优秀员工','迟到王'],
}
if(!emp1.bouns){
    emp1.bouns = emp1.salary * emp1.performance
}
emp1.gender = 'other'
const s:string = JSON.stringify(emp1)
console.log('JSON:',s)
//"JSON:",  "{"name":{"first":"四","last":"张"},"gender":"other","salary":8000,"bouns":28000,"performance":3.5,"badges":["优秀员工","迟到王"]}" 
const emp2 = JSON.parse(s) //生成为新的对象
// 两个对象并不相同
console.log('emp1 === emp2?',emp1 === emp2) //false

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DreamCatcher

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

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

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

打赏作者

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

抵扣说明:

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

余额充值