AngularJS的this詳解

來源:互聯網
上載者:User

標籤:數組   詳解   strong   int   調用   cli   click   interval   規律   

【this詳解】
        
          1、誰最終調用函數,this指向誰。
             ① this指向的,永遠只可能是對象!!!!!!
             ② this指向誰,永遠不取決於this寫在哪!!而是取決於函數在哪調用。
             ③ this指向的對象,我們稱之為函數的上下文context,也叫函數的調用者
         
          2、※※※※※this指向的規律(與函數調用的方式息息相關):
             this指向的情況,取決於函數調用的方式有哪些:
             ① 通過函數名()直接調用:this指向window
             ② 通過對象.函數名()調用的:this指向這個對象
             ③ 函數作為數組的一個元素,通過數組下標調用的:this指向這個數組
             ④ 函數作為window內建函數的回呼函數調用:this指向window
              setInterval  setTimeout 等...
             ⑤ 函數作為建構函式,用new關鍵字調用時:this指向新new出的對象

 

 

function func(){
            console.log(this);
        }
        
        ① 通過函數名()直接調用:this指向window
        func(); this--->window
        
        ② 通過對象.函數名()調用的:this指向這個對象
             狹義對象
            var obj = {
                name:"obj",
                func1 :func
            };
            obj.func1();  this--->obj
            
             廣義對象
            document.getElementById("div").onclick = function(){
                this.style.backgroundColor = "red";
            };  this--->div
        
        ③ 函數作為數組的一個元素,通過數組下標調用的:this指向這個數組
        var arr = [func,1,2,3];
        arr[0]();   this--->arr
        
        ④ 函數作為window內建函數的回呼函數調用:this指向window
        setTimeout(func,1000);// this--->window
        setInterval(func,1000);
        
        ⑤ 函數作為建構函式,用new關鍵字調用時:this指向新new出的對象
        var obj = new func(); //this--->new出的新obj
        
        
 

AngularJS的this詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.