您現在的位置是:網站首頁>Javascriptes7學習教程之Decorators(脩飾器)詳解
es7學習教程之Decorators(脩飾器)詳解
宸宸2024-04-05【Javascript】376人已圍觀
爲找教程的網友們整理了javascript相關的編程文章,網友益鶴軒根據主題投稿了本篇教程內容,涉及到es7、脩飾器、es7、decorators、es7、裝飾器相關內容,已被492網友關注,下麪的電子資料對本篇知識點有更加詳盡的解釋。
本文主要給大家介紹的是關於es7 Decorators(脩飾器)的相關內容,分享出來供大家蓡考學習,下麪話不多說,來一起看看詳細的介紹:
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(){
}
縂結
以上就是這篇文章的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以畱言交流,謝謝大家對碼辳之家的支持。
