本文主要給大家介紹的是關(guān)于es7 Decorators(修飾器)的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說,來一起看看詳細(xì)的介紹:
ES6 Decorators(修飾器)
修飾器(Decorator)是一個(gè)函數(shù),用來修改類的行為。這是ES7的一個(gè)提案,目前Babel轉(zhuǎn)碼器已經(jīng)支持
我們?cè)谟螒虼笮晚?xiàng)目種經(jīng)常會(huì)用到的方法,現(xiàn)在es6直接支持
想要使用Decorator的話需要我們配置一下文件夾,配置一下環(huán)境
npm install babel-plugin-transform-decorators-legacy --save-dev
完事配置一下babelrc文件
"plugins": ["transform-decorators-legacy"]
先說一下裝飾器的特點(diǎn)
裝飾器本質(zhì)是一個(gè)函數(shù)
@hometown hometown()
裝飾對(duì)象可以使用多個(gè)裝飾器
@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é)
以上就是這篇文章的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)VeVb武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答