關於js中this的疑問

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   java   for   

 學習bootstrap.js源碼中被js裡邊的this繞的有點暈

 1 /* ======================================================================== 2  * Bootstrap: alert.js v3.2.0 3  * http://getbootstrap.com/javascript/#alerts 4  * ======================================================================== 5  * Copyright 2011-2014 Twitter, Inc. 6  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE) 7  * ======================================================================== */ 8  9 10 +function ($) {11   ‘use strict‘;12 13   // ALERT CLASS DEFINITION14   // ======================15 16   var dismiss = ‘[data-dismiss="alert"]‘17   var Alert   = function (el) {18     $(el).on(‘click‘, dismiss, this.close)19   }20 21   Alert.VERSION = ‘3.2.0‘22 23   Alert.prototype.close = function (e) {24     var $this    = $(this)  // 疑惑: js類原型方法中的 this 指代的應該是對象執行個體, 可這邊居然給封裝成jq對象 並且在下面調用jq的方法;  注意68行的調用方式就明白了25     var selector = $this.attr(‘data-target‘)26 27     if (!selector) {28       selector = $this.attr(‘href‘)29       selector = selector && selector.replace(/.*(?=#[^\s]*$)/, ‘‘) // strip for ie730     }31 32     var $parent = $(selector)33 34     if (e) e.preventDefault()35 36     if (!$parent.length) {37       $parent = $this.hasClass(‘alert‘) ? $this : $this.parent()38     }39 40     $parent.trigger(e = $.Event(‘close.bs.alert‘))41 42     if (e.isDefaultPrevented()) return43 44     $parent.removeClass(‘in‘)45 46     function removeElement() {47       // detach from parent, fire event then clean up data48       $parent.detach().trigger(‘closed.bs.alert‘).remove()49     }50 51     $.support.transition && $parent.hasClass(‘fade‘) ?52       $parent53         .one(‘bsTransitionEnd‘, removeElement)54         .emulateTransitionEnd(150) :55       removeElement()56   }57 58 59   // ALERT PLUGIN DEFINITION60   // =======================61 62   function Plugin(option) {63     return this.each(function () {64       var $this = $(this)65       var data  = $this.data(‘bs.alert‘)66 67       if (!data) $this.data(‘bs.alert‘, (data = new Alert(this)))68       if (typeof option == ‘string‘) data[option].call($this)    // 原來是這邊通過 call() 將方法的範圍(this)給轉向了 ;69     })70   }71 72   var old = $.fn.alert73 74   $.fn.alert             = Plugin75   $.fn.alert.Constructor = Alert76 77 78   // ALERT NO CONFLICT79   // =================80 81   $.fn.alert.noConflict = function () {82     $.fn.alert = old83     return this84   }85 86 87   // ALERT DATA-API88   // ==============89 90   $(document).on(‘click.bs.alert.data-api‘, dismiss, Alert.prototype.close)91 92 }(jQuery);

附百度上關於js中this的描述:

 

JavaScript:this是什嗎?

定義:this是包含它的函數作為方法被調用時所屬的對象。
說明:這句話有點咬嘴,但一個多餘的字也沒有,定義非常準確,我們可以分3部分來理解它!
1、包含它的函數。2、作為方法被調用時。3、所屬的對象。
看例子:
function to_green(){
this.style.color="green";
}
to_green();
上面函數中的this指的是誰?
分析:包含this的函數是,to_green
該函數作為方法被調用了
該函數所屬的對象是。。?我們知道預設情況下,都是window對象。
OK,this就是指的window對象了,to_green中執行語句也就變為,window.style.color="green"
這讓window很上火,因為它並沒有style這麼個屬性,所以該語句也就沒什麼作用。
我們在改一下。

window.load=function(){
var example=document.getElementById("example");
example.onclick=to_green;
}
這時this又是什麼呢?
我們知道通過賦值操作,example對象的onclick得到to_green的方法,那麼包含this的函數就是onclick嘍,
那麼this就是example引用的html對象嘍。
this的環境可以隨著函數被賦值給不同的對象而改變!
下面是完整的例子:

<script type="text/javascript">
function to_green(){
this.style.color="green";
}
function init_page(){
var example=document.getElementById("example");
example.onclick=to_green;
}
window.onload=init_page;
</script>
<a href="#" id="example">點擊變綠</a>

關於js中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.