毕业即就业系列-TypeScript应用 极速上手(6)-为对象定义方法

毕业即就业系列-TypeScript应用 极速上手(6)-为对象定义方法

为对象定义方法

function registerEmployee(p:any){
    return p
}

const emp1 = registerEmployee({
    name:'james',
    salary:10000,
    bonus:undefined as (number | undefined),
    performance:4.5,
    //对象方法,当然this也有嵌套函数调用的深坑
    updateBonus(){
        if(!this.bonus){
            this.bonus = this.salary * this.performance
        }
    },
})

// 执行对象方法 
emp1.updateBonus()
console.log(emp1)
/* 输出结果:
{
  "name": "james",
  "salary": 10000,
  "bonus": 45000,
  "performance": 4.5
} 

*/
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章