毕业即就业系列-TypeScript应用 极速上手(4)-对象类型

毕业即就业系列-TypeScript应用 极速上手(4)-对象类型

1、概括


毕业即就业系列-TypeScript应用 极速上手(4)-对象类型

思维导图

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
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章