Comments (1)
ES6 Decorators(修饰器)
修饰器(Decorator)是一个函数,用来修改类的行为。这是ES7的一个提案,目前Babel转码器已经支持
我们在游戏大型项目种经常会用到的方法,现在es6直接支持
想要使用Decorator的话需要我们配置一下文件夹,配置一下环境
npm install babel-plugin-transform-decorators-legacy --save-dev
完事配置一下babelrc文件
"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="广灵";
return function(target){
target.home=diqu;
}
}
@hometown("山西")
class...
装饰器修饰 类
function school(target){
console.log("123")
target.schoolName="师徒课堂";
}
function hometown(diqu){
//target.home="广灵";
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(){
}
总结
以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
from web-problem.
Related Issues (20)
- 第一期《meta viewport原理》 HOT 4
- 第二期《域名发散与收敛》 HOT 2
- 第三期《float 与 display:inline-block的区别是什么》 HOT 7
- 第四期《前端跨域解决》 HOT 8
- 第五期《web缓存》 HOT 11
- 第六期《css优化》 HOT 3
- 第七期《WebAssembly给前端带来了什么》 HOT 7
- 第八期《websocket原理与应用》
- 第九期《PWA 是什么、给我们带来了什么?》
- 第十期《作用域链》
- 第十一期《http1.0、http1.1、http2、https》 HOT 4
- 第十三期《javascript版排序二叉树》 HOT 2
- 第十四期《js中数组排序方法》
- 第十五期《css3动画》
- 第十六期《svg学习》
- 第十七期《canvas学习》
- 第十八期《vue.nextTick实现》 HOT 5
- 数据结构/算法 HOT 1
- 为什么vue不需要fiber
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from web-problem.