VUE data內 THIS 各類狀況

var vm = new Vue({
        /* 在方法中,this 表示該方法所屬的對象。
        若是單獨使用,this 表示全局對象。
        在函數中,this 表示全局對象。
        在函數中,在嚴格模式下,this 是未定義的(undefined)。
        在事件中,this 表示接收事件的元素。
        相似 call() 和 apply() 方法能夠將 this 引用到任何對象。
        */
        data: { 
            b: () => {
                console.log(this); 
                //window 箭頭函數體中的 this 對象,是定義函數時的對象,而不是使用函數時的對象。沒有 this、super、arguments 和 new.target 綁定。
                return function(){
                   return "";
                }
            },
            c: function(){
                console.log(this) //vue 在方法中,this 表示該方法所屬的對象。
                return function(){
                    console.log(this) //window  回調方法寫再一個回調方法內,回調方法自己不屬於對象,因此this爲全局得window
                    return "";     
                }
            },
            d : { a : this}, //window  若是單獨使用,this 表示全局對象。
            e : {a :{ a: function(){
                console.log(this)  //observer 在方法中,this 表示該方法所屬的對象。
                return '';
            }}},
            f : {a : function(){
                console.log(this) //observer 在方法中,this 表示該方法所屬的對象。
                return '';
            }},
        }
    })
相關文章
相關標籤/搜索