es7學(xué)習(xí)教程之Decorators(修飾器)詳解
本文主要給大家介紹的是關(guān)于es7 Decorators(修飾器)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說,來一起看看詳細的介紹:
ES6 Decorators(修飾器)
修飾器(Decorator)是一個函數(shù),用來修改類的行為。這是ES7的一個提案,目前Babel轉(zhuǎn)碼器已經(jīng)支持
我們在游戲大型項目種經(jīng)常會用到的方法,現(xiàn)在es6直接支持
想要使用Decorator的話需要我們配置一下文件夾,配置一下環(huán)境
npm install babel-plugin-transform-decorators-legacy --save-dev
完事配置一下babelrc文件
"plugins": ["transform-decorators-legacy"]
先說一下裝飾器的特點
裝飾器本質(zhì)是一個函數(shù)
@hometown hometown()
裝飾對象可以使用多個裝飾器
@hometown("山西")
@school
class Student{
constructor(name){
this.name=name;
}
@studyke("HTML")
study(){
console.log(this.name+" is studying"+this.ke+"!")
}
}
裝飾器可以帶參數(shù)
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(){
}
總結(jié)
以上就是這篇文章的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
JavaScript獲取地址欄參數(shù)的方法實現(xiàn)
這篇文章主要給大家介紹了關(guān)于JavaScript獲取地址欄參數(shù)的方法實現(xiàn),項目中經(jīng)常遇到獲取上個頁面跳轉(zhuǎn)過來獲取當前的參數(shù),文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-07-07
js prototype和__proto__的關(guān)系是什么
這篇文章主要介紹了js prototype和__proto__的關(guān)系是什么,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
jstree創(chuàng)建無限分級樹的方法【基于ajax動態(tài)創(chuàng)建子節(jié)點】
這篇文章主要介紹了jstree創(chuàng)建無限分級樹的方法,結(jié)合實例形式分析了jstree基于ajax結(jié)合asp.net后臺動態(tài)創(chuàng)建子節(jié)點實現(xiàn)無限分級樹效果的相關(guān)步驟與操作技巧,需要的朋友可以參考下2016-10-10

