亚洲av无码乱码国产一区二区,午夜理论片yy44880影院,午夜久久久久久禁播电影,熟睡人妻被讨厌的公侵犯

13
2017/05

JS中精選this關(guān)鍵字的指向規(guī)律你了解嗎?

發(fā)布時(shí)間:2017-05-13 17:58:29
發(fā)布者:jiangbing
瀏覽量:
0

一、首先要明確:

1、誰(shuí)最終調(diào)用函數(shù),this指向誰(shuí)。

2、this指向的永遠(yuǎn)只可能是對(duì)象!

3、this指向誰(shuí)永遠(yuǎn)不取決于this寫(xiě)在哪,而取決于函數(shù)在哪里調(diào)用!

4、this指向的對(duì)象,我們稱之為函數(shù)的上下文context,也叫做函數(shù)的調(diào)用者是誰(shuí)!

二、this指向的規(guī)律(與函數(shù)調(diào)用的方式息息相關(guān))

this指向的情況取決于函數(shù)調(diào)用的方式有哪些(總結(jié)如下):

1、通過(guò)函數(shù)名()直接調(diào)用--this 指向window;

function func(){
         console.log(this);
         }
func();

2、通過(guò)對(duì)象.函數(shù)名()調(diào)用的--this指向這個(gè)對(duì)象

狹義對(duì)象: this指向--obj

var obj={
      name:"obj",
      func1:func
     };
     obj.func1();

 廣義對(duì)象: this指向--div

 document.getElementById("div").onclick=function(){this.style.backgroundColor="red";}

3、this指向——數(shù)組arr

var arr=[func,1,2,3];
     arr[0]();

4、函數(shù)作為window內(nèi)置函數(shù)的回調(diào)函數(shù)調(diào)用,this指向window setInterval,setTimout等

setInterval(func,1000);         
setTimeout(func,1000)

5、函數(shù)作為構(gòu)造函數(shù),用new關(guān)鍵字調(diào)用時(shí):this指向新定義的對(duì)象obj

 var obj=new func();

6、通過(guò)call、apply、bind調(diào)用,this指向我們規(guī)定的對(duì)象。

Func.call(obj,參數(shù)一,參數(shù)2,參數(shù)3.。。。)

Func.allply(obj,[ 參數(shù)一,參數(shù)2,參數(shù)3.。。。])

Func.bind(obj)( 參數(shù)一,參數(shù)2,參數(shù)3)   var f = func.bind(obj).   f(…….);


關(guān)鍵詞:
返回列表