溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

es7學習教程之Decorators(修飾器)詳解

發布時間:2020-10-25 13:14:07 來源:腳本之家 閱讀:200 作者:daisy 欄目:web開發

本文主要給大家介紹的是關于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(){

  } 

總結

以上就是這篇文章的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女