簡說一下coffeescript的constructor是如何導致Backbone.View的事件無法正常工作的.

來源:互聯網
上載者:User

在繼承方面,js還是弱項呀。發現在繼承的時候constructor和initialize之分。網上文章沒有說明二者關係。看了源碼才發現二者的區別呀。


首先我用coffeescript來實現js的繼承,過程中發現一個問題。

就是通過用Backbone來構造單頁面程式的時候,如果父類或子類中定義了constructor而沒有super。那麼很高興的告訴你,在子類註冊的events是不工作滴。

這裡提前告訴你是因為父類中如果沒有super,擇不會執行預設的構造器來構造Backbone.View類。而這個類在構造的時候工作是初始化類中的initialize和綁定events事件(基於Jquery的delegate)

不信你試試看。例如:

 

class OA extends Backbone.Viewconstructor:->#super;console.log "OA constructor";initialize :->console.log "OA init";class A extends OAconstructor:->#super;console.log "A constructor";initialize:->console.log "A init";events : "click #btn1" : "btnClick"btnClick : ->console.log "btn click 1";

就是這樣你在A中定義了constructor,而沒有執行super,或者你在A中定義了construto且定義了super而在OA中的construtor構造中沒有super。子類A中的events就沒有執行。

根據這個一步步分析原因。

1:coffeescript在定義類中,如果類中沒有構造constructor,則它會自己構造constructor。如果指定了constructor那麼就會按照指定的來執行.

源碼:

指定了constructor

 

OA = (function(){function e(){console.log("OA constructor")}})(Backbone.View)

未定義construto或定義並且super了

__extend = function(t,n){function i(){this.constructor = t;}for(var r in n) e.call(n,r) && (t[r]=n[r]);i.prototype = n.prototype;t.prototype = new i;t.__super__ = n.prototype;return t;}OA = (function(n){function e(){console.log("OA constructor")_ref = e.__super__.constructor.apply(this,arguments);//這個等價於Backbone.View.prototype.constructor.apply(this,arguments);}__extend(e,n);//這個是用來繼承的。})(Backbone.View)

ok刨到這裡還得繼續往祖墳刨,看下這個源碼再解釋。


Backbone.View不用說。上面就幾個屬性和方法。可以看到為什麼類定義了initialize後自動調用的原因了吧。


還有你也知道為什麼view中events定義後就可以處理事件了吧(因為delegateEvents初始化了)。

這是部分代碼,如果有js進階教程經驗的可以看下backbone.js關於view類的源碼。

 



Backbone.View.prototype是通過underscore的extend繼承Events和opt拓展屬性群組合而成的(如果不懂underscore.js的extend請看這裡:http://underscorejs.org/#extend)。View.prototype==Backbone.View因為function是引用調用,公用一片記憶體,

所以View.prototype的變化會影響到Backbone.View的prototype.

再看e.__super == Backbone.View.prototype之後Backbone.View.prototype.constructor就是由Backbone.View的構造的,所以e.__super__.constructor則就是Backbone.View.apply(this,arguments);所以執行Backbone.View就有了一些關於事件,initialize函數的一些初始化。在類中如果super了。就會執行parent的constructor。直到執行繼承Backbone.View的constructor即可。

不知道大家明白了與否。

也就是super執行的流程如下:

child(constructor)->parent(constructor).......//如果constructor中有super.就是這個執行順序。只要繼承類中某個類沒有super。擇constructor就停止了構造。

ok,到了這裡,我想大概都應該明白了。所以如果以後涉及到繼承。請謹慎定義constructor。如果你需要在子類中定義事件,而有繼承了父類。那你一定要檢查一下父類的constructor和子類的constructor是否super了(coffee中的super就是調用父類的建構函式,然-child.__super__.constructor.apply(this,arguments)).否則你子類的events對象就不會初始化,從而導致點擊無效果。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.