修饰器(Decorator)是一个函数,用来修改类的行为。这是ES7的一个提案,目前Babel转码器已经支持es6
咱们在游戏大型项目种常常会用到的方法,如今es6直接支持
想要使用Decorator的话须要咱们配置一下文件夹,配置一下环境npm
npm install babel-plugin-transform-decorators-legacy --save-dev
完事配置一下babelrc文件babel
"plugins": ["transform-decorators-legacy"]
先说一下装饰器的特色函数
@hometown hometown()
@hometown("山西") @school class Student{ constructor(name){ this.name=name; } @studyke("HTML") study(){ console.log(this.name+" is studying"+this.ke+"!") } }
function hometown(diqu){ //target.home="xx"; return function(target){ target.home=diqu; } } @hometown("山西") class...
function school(target){ console.log("123") target.schoolName="xxxx"; } function hometown(diqu){ //target.home="xx"; return function(target){ target.home=diqu; } } function studyke(kemu){ return function(target){ target.ke=kemu; } } @hometown("山西") @school class Student{ constructor(name){ this.name=name; } @studyke("HTML") study(){ console.log(this.name+" is studying"+this.ke+"!") } } console.log(Student.schoolName); console.log(Student.home); let l=new Student("xiaoA"); l.study(); @school function Teacher(){ }