久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      JavaScript的this指向哪里

      this指向:1、普通函數(shù)或作為對象屬性,指向window對象;2、事件綁定中,指向綁定事件的元素;3、構(gòu)造函數(shù)中,指向類的實例;4、箭頭函數(shù)中,指向其最近父級上下文中的this;5、call/apply/bind中,指向傳入的第一個參數(shù)。

      JavaScript的this指向哪里

      本教程操作環(huán)境:windows7系統(tǒng)、javascript1.8.5版、Dell G3電腦

      JavaScriptthis指向分為以下幾種情況:

      • 普通函數(shù)或作為對象屬性
      • 事件綁定
      • 構(gòu)造函數(shù)
      • 箭頭函數(shù)
      • call/apply/bind指定

      下面我們來進(jìn)行一一介紹

      普通函數(shù)或作為對象屬性

      this取決于方法執(zhí)行前面是否有“點(diǎn)”,有“點(diǎn)”的話,“點(diǎn)”前面是誰this就是誰,如果沒有點(diǎn)的話,this指向window

      const fn = function () {   console.log(this); };  const obj = { name: 'OBJ', fn };  fn();  obj.fn();  const fn1 = obj.fn; fn1();

      answer:

      1. window 2. {name: 'OBJ', fn: function() {console.log(this)}} // obj 3. window

      可以看到函數(shù)作為對象的屬性被調(diào)用的時候,其this指向調(diào)用該函數(shù)的對象,否則其this指向window

      事件綁定

      在進(jìn)行事件綁定的時候,事件綁定函數(shù)中的this是綁定事件的元素:

      // 假設(shè)頁面中有id為button的button元素 // var x = 100; window.x = 100; const fn = function () {   console.log(this.x); }; const obj = { x: 200, fn }; const $button = document.getElementById('button'); $button.x = 300;  obj.fn(); const fn1 = obj.fn; fn1();  $button.addEventListener('click', fn); $button.addEventListener('mouseenter', obj.fn);  $button.addEventListener('mouseleave', function () {obj.fn();});

      answer:

      1. 200 2. 100 3. 點(diǎn)擊button時:300 4. 鼠標(biāo)移入button時:300 5. 鼠標(biāo)移出時:200

      但是需要注意的是,這里我們是在用戶點(diǎn)擊時,瀏覽器幫我們將點(diǎn)擊事件的this指向綁定該事件的DOM元素。如果通過代碼來觸發(fā)對應(yīng)的事件的話,我們可以通過call/apply/bind來指定其this

      $button.click.call() // this為window,打印結(jié)果為100

      構(gòu)造函數(shù)(new Fn)

      構(gòu)造函數(shù)(new Fn)執(zhí)行,函數(shù)中的this是當(dāng)前類的實例,這是new關(guān)鍵字幫我們做到的:

      var x = 100; const Fn = function () {   this.x = 200;   console.log(this.x); };  const fn = new Fn();

      answer:

      1. 200

      箭頭函數(shù)

      箭頭函數(shù)中沒有自身的this,所用到的this都是其最近父級上下文中的this

      const fn = function () {   console.log(this);   setTimeout(() => {     console.log(this);   }, 1000);   setTimeout(function () {     console.log(this);   }); };  const obj = { x: 100, fn };  obj.fn();

      answer:

      1. {x:100, fn: function() {...}} // obj 2. window 3. {x:100, fn: function() {...}} // obj

      call/apply/bind改變this指向

      call/apply/bind傳入的第一個參數(shù)即為函數(shù)的this

      var x = 100; const obj = { x: 200, y: 200 }; const fn = function () {   console.log(this.x); };  fn(); fn.call(obj); fn.apply(obj);  const fixedThisFn = fn.bind(obj); fixedThisFn();

      answer:

      1. 100 2. 200 3. 200 4. 200
      • call在執(zhí)行時,第一個參數(shù)為this指向,之后的參數(shù)為fn執(zhí)行時的參數(shù)
      • apply在執(zhí)行時,第一個參數(shù)為this指向,之后的參數(shù)為fn執(zhí)行時的參數(shù)組成的數(shù)組,數(shù)組的每一項會和fn的每一個參數(shù)進(jìn)行對應(yīng)
      • bind在執(zhí)行時,第一個參數(shù)為預(yù)先傳入this指向,之后的參數(shù)為實際調(diào)用fn前預(yù)先傳入的參數(shù),返回值為一個函數(shù)fixedThisFn,fixedThisFn內(nèi)部會調(diào)用fn并指定其this指向

      為了更深入的理解call/apply/bind是如何改變函數(shù)中this指向的,下面我們分別模擬實現(xiàn)這三個函數(shù)

      call/apply/bind源碼實現(xiàn)

      根據(jù)前面的介紹,我們知道:當(dāng)函數(shù)作為對象屬性被調(diào)用時,this指向調(diào)用該函數(shù)的對象

      const obj = { x: 100, fn () {console.log(this);} }; obj.fn(); // {x: 100, fn: function() {...}} => obj

      利用JavaScript這個特性,我們可以將執(zhí)行的函數(shù)作為call/apply的第一個參數(shù)context的屬性,然后通過context來調(diào)用該屬性對應(yīng)的函數(shù),函數(shù)的this便指向了context

      call的源碼模擬如下:

      Function.prototype.myOwnCall = function (context, ...args) {   const uniqueKey = new Date().getTime();   // this為調(diào)用call方法的函數(shù)   context[uniqueKey] = this;   // 作為對象的方法被對象調(diào)用,this指向該對象context   const result = context[uniqueKey](...args);   delete context[uniqueKey];   return result; };

      到這里,有的小伙伴可能已經(jīng)發(fā)現(xiàn)了,如果call/apply傳入的context不是對象呢?

      首先我們看下mdncall方法的第一個參數(shù)的描述:

      語法:function.call(thisArg, arg1, arg2, ...)
      * thisArg
      可選的。在function函數(shù)運(yùn)行時使用的this值。請注意,this可能不是該方法看到的實際值:如果這個函數(shù)處于非嚴(yán)格模式下,則指定nullundefined時會自動替換為指向全局對象,原始值會被包裝

      接下來,我們對myOwnCall方法的第一個參數(shù)做如下處理:

      function translateToObject (context) {   // 可以通過 == 進(jìn)行判斷 context == null   // null == undefined  => 2個等號是成立的   // null,undefined => window   if (typeof context === 'undefined' || context === null) {     context = window;   } else if (typeof context === 'number') { // 原始值轉(zhuǎn)換為包裝對象     context = new Number(context);   } else if (typeof context === 'string') {     context = new String(context);   } else if (typeof context === 'boolean') {     context = new Boolean(context);   }   return context; }

      myOwnCall方法中調(diào)用該函數(shù):

      Function.prototype.myOwnCall = function (context, ...args) {   context = translateToObject(context);   const uniqueKey = new Date().getTime();   // this為調(diào)用call方法的函數(shù)   context[uniqueKey] = this;   // 作為對象的方法被對象調(diào)用,this指向該對象context   const result = context[uniqueKey](...args);   delete context[uniqueKey];   return result; };

      apply的實現(xiàn)與call基本相同,只不過第二個參數(shù)是一個數(shù)組:

      Function.prototype.myOwnBind = function (context, paramsArray) {   context = translateToObject(context);   const uniqueKey = new Date().getTime();   // this為調(diào)用call方法的函數(shù)   context[uniqueKey] = this;   // 作為對象的方法被對象調(diào)用,this指向該對象context   const result = context[uniqueKey](...paramsArray);   delete context[uniqueKey];   return result; };

      相比于call/apply,bind函數(shù)并沒有立即執(zhí)行函數(shù),而是預(yù)先傳入函數(shù)執(zhí)行時的this和參數(shù),并且返回一個函數(shù),在返回的函數(shù)中執(zhí)行調(diào)用bind函數(shù)并將預(yù)先傳入的this和參數(shù)傳入

      bind的源碼模擬:

      Function.prototype.myOwnBind = function (context, ...outerArgs) {   const fn = this;   return function (...innerArgs) {     return fn.call(context, ...outerArgs, ...innerArgs);   }; };

      精簡版如下:

      Function.prototype.myOwnBind = (context, ...outerArgs) => (...innerArgs) => this.call(context, ...outerArgs, ...innerArgs);

      這里并沒有實現(xiàn)通過new操作符來執(zhí)行fn.bind(context)的操作,如果想知道其詳細(xì)的實現(xiàn)過程,可以看我的這篇文章: JS進(jìn)階-手寫bind

      在深入理解call/apply/bind的實現(xiàn)原理后,我們嘗試完成下面的測試:

      function fn1 () {console.log(1);} function fn2 () {console.log(2);} fn1.call(fn2);  fn1.call.call(fn2);  Function.prototype.call(fn1); Function.prototype.call.call(fn1);

      answer:

      1. 1 2. 2 3. 什么都不輸出 4. 1

      這里我們根據(jù)call的源碼來進(jìn)行推導(dǎo)一下Function.prototype.call.call(fn1),其它的執(zhí)行過程類似:

      // 1. 首先會將Function.prototype.call作為一個函數(shù)來執(zhí)行它原型上的call方法 // 所以call方法內(nèi)部: //    this => Function.prototype.call //    context => fn1 // 通過對象的屬性來執(zhí)行方法改變this指向 //    fn1[uniqueKey] = this(Function.prototype.call) //    fn1[uniqueKey]() // 執(zhí)行 Function.prototype.call方法,但是this是context // 2. 在this為fn1的情況下執(zhí)行Function.prototype.call方法 // 所以call方法內(nèi)部: //    this => fn1 //    context => window // 通過對象的屬性來改變this指向 //    window[uniqueKey] = fn1 //    window[uniqueKey]() // 執(zhí)行fn1(),但是this是window

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號