亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于JavaScript中的this指向問(wèn)題總結(jié)篇

 更新時(shí)間:2017年07月23日 08:50:26   作者:PR都像梅超風(fēng)  
在小編面試過(guò)程中經(jīng)常會(huì)遇到j(luò)avascript中this指向問(wèn)題,可以說(shuō)是前端面試必問(wèn),下面小編給大家總結(jié)了一下js中this的指向,感興趣的朋友一起學(xué)習(xí)吧

在javascript中this的指向一直是前端同事的心頭病,也同時(shí)是各面試題的首選,現(xiàn)在我們就來(lái)總結(jié)一下js中this的指向。首先需要了解一下幾個(gè)概念:

1:全局變量默認(rèn)掛載在window對(duì)象下

2:一般情況下this指向它的調(diào)用者

3:es6的箭頭函數(shù)中,this指向創(chuàng)建者,并非調(diào)用者

4:通過(guò)call、apply、bind可以改改變this的指向

下面我們具體分析一下

1:在函數(shù)調(diào)用時(shí)

 ?。ǚ菄?yán)格模式)

const func = function () {
    console.log(this);
    const func2 = function () {
      console.log(this);
    };
    func2(); //Window
  };
  func(); //Window

  ?。▏?yán)格模式)

'use strict'
  const func = function () {
    console.log(this);
    const func2 = function () {
      console.log(this);
    };
    func2(); //undefined
  };
  func(); //undefined

     結(jié)合第四和第一兩條規(guī)則:func這個(gè)函數(shù)是全局的,默認(rèn)掛載在window對(duì)象下,this指向它的調(diào)用者即window,所以輸出window對(duì)象,但是在嚴(yán)格模式下,this不允許指向全局變量window,所以輸出為undefined(func2在函數(shù)直接調(diào)用時(shí)默認(rèn)指向了全局window,其實(shí)這屬于javascript設(shè)計(jì)上的缺陷,正確的設(shè)計(jì)方式是內(nèi)部函數(shù)的this 應(yīng)該綁定到其外層函數(shù)對(duì)應(yīng)的對(duì)象上,為了規(guī)避這一設(shè)計(jì)缺陷,聰明的 JavaScript 程序員想出了變量替代的方法,約定俗成,該變量一般被命名為 that。這種方式在接下來(lái)會(huì)講到)。

2:作為對(duì)象方法

const user = {

    userName: '小張',
    age: 18,
    selfIntroduction: function () {
      const str = '我的名字是:' + this.userName + ",年齡是:" + this.age;
      console.log(str);

      const loop = function () {
        console.log('我的名字是:' + this.userName + ",年齡是:" + this.age);
      };

      loop();   //我的名字是:undefined,年齡是:undefined

    }
  };

  user.selfIntroduction();  //我的名字是:小張,年齡是:18

    按照咱的第一條規(guī)則,this指向他的調(diào)用者,selfIntroduction()方法的調(diào)用者是user,所以在selfIntroduction()方法內(nèi)部this指向了他的父對(duì)象即user,而loop方法輸出的為undefined的原因就是我在上面所說(shuō)的javascript的設(shè)計(jì)缺陷了,在這種情況下,我們通常選擇在selfIntroduction()方法里將this緩存下來(lái)。

const user = {
    userName: '小張',
    age: 18,
    selfIntroduction: function () {
      const str = '我的名字是:' + this.userName + ",年齡是:" + this.age;
      console.log(str);

      const that=this;

      const loop = function () {
        console.log('我的名字是:' + that.userName + ",年齡是:" + that.age);
      };

      loop();   //我的名字是:小張,年齡是:18

    }
  };

  user.selfIntroduction();  //我的名字是:小張,年齡是:18

此時(shí)loop的this指向就理想了。

const user={

    userName:'小張',
    age:18,
    selfIntroduction:function(){
      const str='我的名字是:'+this.userName+",年齡是:"+this.age;
      console.log(str); 
    }
  };

  const other =user.selfIntroduction;
  other(); //我的名字是:undefined,年齡是:undefined

  const data={
    userName:'小李',
    age:19,
  };
  data.selfIntroduction=user.selfIntroduction;
  data.selfIntroduction(); //我的名字是:小李,年齡是:19

  在看這段代碼,將selfIntroduction()賦值給了全局變量other,調(diào)用other()方法,other掛載在全局函數(shù)window對(duì)象下,window對(duì)象下沒(méi)有userName 和 age 這兩個(gè)屬性,所以輸出為undefined。第二段代碼,申明了data對(duì)象,包含了username和age屬性,記住我們的第二條規(guī)則一般情況下this指向它的調(diào)用者,大家就明白了,data是selfIntroduction()的函數(shù)的調(diào)用者,所以輸出了data的userName和age。

3:在html里作為事件觸發(fā)

<body>
  <div id="btn">點(diǎn)擊我</div>
</body>
     const btn=document.getElementById('btn');
    btn.addEventListener('click',function () {
      console.log(this); //<div id="btn">點(diǎn)擊我</div>
    })

在種情況其實(shí)也是遵循了第二條規(guī)則一般情況下this指向它的調(diào)用者,this指向了事件的事件源即event。

4:new關(guān)鍵字(構(gòu)造函數(shù))

const fun=function(userName){
    this.userName=userName;
  }
  const user=new fun('郭德綱');  
  console.log(user.userName); //郭德綱

 這個(gè)就不多贅述了,new關(guān)鍵字構(gòu)造了一個(gè)對(duì)象實(shí)例,賦值給了user,所以u(píng)serName就成為了user對(duì)象的屬性。

5:es6(箭頭函數(shù))

const func1=()=>{
    console.log(this); 
  };
  func1(); //Window
const data={
    userName:'校長(zhǎng)',
    selfIntroduction:function(){
      console.log(this); //Object {userName: "校長(zhǎng)", selfIntroduction: function}
      const func2=()=>{
        console.log(this); //Object {userName: "校長(zhǎng)", selfIntroduction: function}
      }

      func2();
    }
  }
  data.selfIntroduction();

  大家在看看我開(kāi)頭說(shuō)的第三條準(zhǔn)則:es6的箭頭函數(shù)中,this指向創(chuàng)建者,并非調(diào)用者,fun1 在全局函數(shù)下創(chuàng)建,所以this指向全局window,而fun2在對(duì)象data下創(chuàng)建,this指向data對(duì)象,所以在func2函數(shù)內(nèi)部this指向data對(duì)象,個(gè)人認(rèn)為es6的箭頭函數(shù)的this指向是對(duì)我上面所說(shuō)的javascript設(shè)計(jì)缺陷的改進(jìn),(個(gè)人認(rèn)知)。

6:改變this的指向

  call、apply、bind這三個(gè)函數(shù)是可以人為的改變函數(shù)的this指向的,在這里就不多說(shuō)這三者的區(qū)別了,在往后的博客里我會(huì)詳細(xì)解釋這三者的區(qū)別的?,F(xiàn)在先拿一個(gè)來(lái)舉一個(gè)例子

const func=function(){
   console.log(this);
 }; 
 func(); //window
 func.apply({userName:"郭德綱"}); //Object {userName: "郭德綱"}

   這三個(gè)方法都是可以人為的改變this的指向,區(qū)別是call、apply會(huì)將該方法綁定this之后立即執(zhí)行,而bind方法會(huì)返回一個(gè)可執(zhí)行的函數(shù)。

說(shuō)這很多總結(jié)起來(lái)就是我開(kāi)頭說(shuō)的4點(diǎn)

1:全局變量默認(rèn)掛載在window對(duì)象下

2:一般情況下this指向它的調(diào)用者

3:es6的箭頭函數(shù)中,this指向創(chuàng)建者,并非調(diào)用者

4:通過(guò)call、apply、bind可以改改變this的指向

說(shuō)實(shí)話(huà)第一次寫(xiě)博客,確實(shí)挺忐忑的,會(huì)不會(huì)有人看我的博客?會(huì)不會(huì)寫(xiě)的不正確?……想好多了,總結(jié)了:不好的地方歡迎指正。

以上所述是小編給大家介紹的關(guān)于JavaScript中的this指向問(wèn)題總結(jié)篇,希望對(duì)大家有所幫助,如果大家有任何問(wèn)題,歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

最新評(píng)論