当前位置: 首页 > Web前端 > HTML5

Decorator

时间:2023-04-05 15:10:14 HTML5

//装饰器本质上是一个函数//被装饰的对象可以使用多个装饰器//装饰器可以带参数//装饰器修改类、实例方法//aop设计思想(日志、邮件发送)functionschool(target){target.schoolName="快手live";}functionhometown(diqu){returnfunction(target){target.home=diqu;}}functionstudyke(kemu){returnfunction(target){目标.ke=kemu;}}@hometown("root")@schoolclassStudent{constructor(name){this.name=name;}@studyke("jquery")study(){console.log(this.name+"正在看"+this.ke);}}console.log(Student.schoolName);//显示快手居住。console.log(Student.home);//显示根。letl=newStudent("曹魏");l.study();//显示曹魏再看jquery。@schoolclassTeacher{}console.log(Teacher.schoolName);//显示快手直播。