• <noscript id="ggggg"><dd id="ggggg"></dd></noscript>
    <small id="ggggg"></small> <sup id="ggggg"></sup>
    <noscript id="ggggg"><dd id="ggggg"></dd></noscript>
    <tfoot id="ggggg"></tfoot>
  • <nav id="ggggg"><cite id="ggggg"></cite></nav>
    <nav id="ggggg"></nav>
    成人黃色A片免费看三更小说,精品人妻av区波多野结衣,亚洲第一极品精品无码,欧美综合区自拍亚洲综合,久久99青青精品免费观看,中文字幕在线中字日韩 ,亚洲国产精品18久久久久久,黄色在线免费观看

    JavaScript 中的 call()、apply()、bind() 的詳解

    2018-4-22    seo達人

    如果您想訂閱本博客內容,每天自動發到您的郵箱中, 請點這里

    三種方法的作用

    在 JavaScript 中

    1. callapply 和 bind 是 Function 對象自帶的三個方法,都是為了改變函數體內部 this 的指向。
    2. call、apply 和 bind 三者第一個參數都是 this 要指向的對象,也就是想指定的上下文。
    3. call、apply 和 bind 三者都可以利用后續參數傳參。
    4. bind 是返回對應 函數,便于稍后調用;apply 、call 則是立即調用 。
    舉個栗子
    function fruits() {}
    
    fruits.prototype = {
       color: 'red',
       say: function() { console.log('My color is ' + this.color); 
       }
    } var apple = new fruits;
    apple.say(); // 此時方法里面的this 指的是fruits // 結果: My color is red
        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    如果我們有一個對象 banana= {color : 'yellow'} ,我們不想重新定義 say 方法,那么我們可以通過 call 或 apply 用 apple 的 say 方法:

    var banana = { color: 'yellow' };
    apple.say.call(banana); // 此時的this的指向已經同過call()方法改變了,指向的是banana,this.color就是banana.color='yellow'; // 結果是My color is yellow 
    
    apple.say.apply(banana); // 同理,此時的this的指向已經同過apply()方法改變了,指向的是banana,this.color就是banana.color ='yellow'; // 結果是My color is yellow
    
    apple.say.apply(null); // nullwindow下的,此時,this 就指向了window ,但是window下并沒有clolr這個屬性,因此this.clolr就是window.color=undefined; // 結果是My color is undefined
        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    call 和 apply 的區別

    二者的作用完全一樣,知識接受 參數 的方式不太一樣。

    call 是把參數按順序傳遞進去,而 apply 則是把參數放在 數組 里面。

    var array1 = [12,'foo',{name:'Joe'},-2458]; var array2 = ['Doe' , 555 , 100]; Array.prototype.push.call(array1, array2); // 這里用 call 第二個參數不會把 array2 當成一個數組,而是一個元素 // 等價于 array1.push("'Doe' , 555 , 100"); // array1.length=5; Array.prototype.push.apply(array1, array2); // 這里用 apply 第二個參數是一個數組 // 等價于:  array1.push('Doe' , 555 , 100); // array1.length=7;
        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    類(偽)數組使用數組方法
    var divElements = document.getElementsByTagName('div'); // 雖然 divElements 有 length 屬性,但是他是一個偽數組,不能使用數組里面的方法 Array.isArray(divElements);// false var domNodes = Array.prototype.slice.call(document.getElementsByTagName('div')); // 將數組對象 Array 里的 this 指向偽數組 document.getElementsByTagName('div'),  // slice() 方法可從已有的數組中返回選定的元素,不傳參數是,返回整個數組  Array.isArray(domNodes);// true
        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    驗證一個對象的類型可以用
    Object.prototype.toString.call(obj)
        
    • 1
    bind() 方法

    bind() 方法會創建一個 新函數,稱為綁定函數,當調用這個綁定函數時,綁定函數會以創建它時傳入 bind() 方法的第一個參數 作為 this,傳入 bind() 方法的 第二個以及以后的參數加上綁定函數運行時本身的參數按照順序作為原函數的參數來調用原函數。

    注意bind()方法創建的函數不會立即調用,在下面的例子中,最后 func() 才調用了函數,這是它與 callapply的區別。

    var bar = function(){ console.log(this.x);
    } var foo = {
        x:3 }
    bar(); // undefined var func = bar.bind(foo); //此時this已經指向了foo,但是用bind()方法并不會立即執行,而是創建一個新函數,如果要直接調用的話 可以bar.bind(foo)() func(); // 3
        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在 Javascript 中,多次 bind() 是無效的。更深層次的原因, bind() 的實現,相當于使用函數在內部包了一個 call / apply ,第二次 bind() 相當于再包住第一次 bind() ,故第二次以后的 bind 是無法生效的。

    var bar = function(){ console.log(this.x);
    } var foo = {
      x:3 } var sed = {
      x:4 } var func = bar.bind(foo).bind(sed);
    func(); //3 var fiv = {
      x:5 } var func = bar.bind(foo).bind(sed).bind(fiv);
    func(); //3

    日歷

    鏈接

    個人資料

    藍藍設計的小編 http://www.lzhte.cn

    存檔

    主站蜘蛛池模板: 永久免费精品性爱网站| 99久久精品国产片| 最新日韩精品视频在线| 十八禁午夜福利免费网站| 国产免费播放一区二区| 成人观看欧美特黄A片| 久久国产精品亚洲va麻豆| 中文字幕一区二区二三区四区| 人人妻人人澡人人爽人人精品97| 日本高清视频www夜色资源| 中文字幕日韩一区二区不卡| 欧美精品亚洲精品日韩精品| 色九月亚洲综合网| 国产精品久久亚洲一区二区| 青青草原国产精品啪啪视频| 天天做夜夜爽熟女久久| 久久精品蜜芽亚洲国产AV| 国产在线精品一区在线观看| 艹b视频在线观看| 亚洲深深色噜噜狠狠爱网站| jizzjizz日本高潮喷水| 日本精品αv中文字幕| 亚洲AV永久天码精品天堂DL| 蜜臀午夜av一二三区| 国产理论亚洲天堂av| 国产精品爆乳在线播放| 日韩在线中文字幕| 在线观看国产一区亚洲bd| 日韩AV有码无码一区二区三区| 91久久天天躁狠狠躁夜夜| 在线无码不卡app| 91一区二区三区蜜桃| 精品无码一区二区三区在线视频| 亚洲中文久久久久无码| 91全国偷拍免费视频| 国产在亚洲线视频观看| 国产区福利小视频在线观看尤物| 亚洲av乱码国产精品色 | 亚洲欧美综合人成野草| 欧洲精品色在线观看| 国产AV嫩草研究院|