es7学习教程之Decorators(修饰器)详解
本文主要给大家介绍的是关于es7Decorators(修饰器)的相关内容,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍:
ES6Decorators(修饰器)
修饰器(Decorator)是一个函数,用来修改类的行为。这是ES7的一个提案,目前Babel转码器已经支持
我们在游戏大型项目种经常会用到的方法,现在es6直接支持
想要使用Decorator的话需要我们配置一下文件夹,配置一下环境
npminstallbabel-plugin-transform-decorators-legacy--save-dev
完事配置一下babelrc文件
"plugins":["transform-decorators-legacy"]
先说一下装饰器的特点
装饰器本质是一个函数
@hometownhometown()
装饰对象可以使用多个装饰器
@hometown("山西")
@school
classStudent{
constructor(name){
this.name=name;
}
@studyke("HTML")
study(){
console.log(this.name+"isstudying"+this.ke+"!")
}
}
装饰器可以带参数
functionhometown(diqu){
//target.home="广灵";
returnfunction(target){
target.home=diqu;
}
}
@hometown("山西")
class...
装饰器修饰类
functionschool(target){
console.log("123")
target.schoolName="师徒课堂";
}
functionhometown(diqu){
//target.home="广灵";
returnfunction(target){
target.home=diqu;
}
}
functionstudyke(kemu){
returnfunction(target){
target.ke=kemu;
}
}
@hometown("山西")
@school
classStudent{
constructor(name){
this.name=name;
}
@studyke("HTML")
study(){
console.log(this.name+"isstudying"+this.ke+"!")
}
}
console.log(Student.schoolName);
console.log(Student.home);
letl=newStudent("xiaoA");
l.study();
@school
functionTeacher(){
}
总结
以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。
热门推荐
10 医师考试祝福语毕业简短
11 雷州人结婚祝福语简短
12 二零二一年祝福语简短
13 心媳妇生日祝福语简短
14 辞职发给公司祝福语简短
15 过年祝福语闺蜜简短
16 儿童励志寄语简短祝福语
17 小班老师六一祝福语简短
18 回复姐姐生日祝福语简短