Decorator(裝飾器,修飾器,實例方法)

Decorator(裝飾器,修飾器,實例方法)jquery

Decorator:函數

function school(target){
        target.schoolName="師徒課堂";
    }
        function hometown(diqu){
            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("jquery")
            study(){
                console.log(this.name+"在學習"+this.ke);
            }
        }
        console.log(Student.schoolName);//打印師徒課堂.
        console.log(Student.home);//打印廣靈縣.
            
        let l = new Student("宋澤");
        l.study();//打印宋澤在學習jquery.
            
        @school
        class Teacher {
            
        }
        console.log(Teacher.schoolName);//打印師徒課堂.

  • 註釋:學習

    1.裝飾器本質是一個函數;
        2.裝飾對象能夠使用多個裝飾器;
        3.裝飾器能夠帶參數;
        4.裝飾器修飾類,實例方法;
        5.aop 設計思想(log,郵件發送)。
相關文章
相關標籤/搜索