跪求分析代碼, this.handlers[type].push(handler)是什么意思
on:function(type,handler){ ?? ?????? ?if(typeof?this.handlers[type]=="undefined"){ ?? ?????? ? this.handlers[type]=[]; ?? ?????? ?} ?? ?????? ?this.handlers[type].push(handler); ?? ??????}, ? ?? ??????fire:function(type,data){ ?? ?????? ?????if(this.handlers[type]?instanceof?Array){ ?? ?????? ????? var?handlers=this.handlers[type]; ?? ?????? ????? for(var?i=0,len=handlers.length;i<len;i++){ ?? ?????? ????? handlers[i](data); ?? ?????? ????? } ?? ?????? ?????} ?? ??????},
請教分析代碼, this.handlers[type].push(handler)是什么意思?視頻里說“需要執行的回調函數push到這個數組中”,那這個數組是handlers[type]?? 搞不懂參數里面的type是什么類型= =,求大神詳細解答fire和on里面的代碼。。
2016-04-01
handlers是一個對象
type相當于key值 而value值為回調函數handler的數組
每當傳入新的type時 對應的數組還沒初始化?
如果傳入相同的type,則添加到對應的數組中
2016-04-12
其實我也不明白那個typeof加在哪兒什么意思,但是你看哦,構造函數里面的this.handlers是空的,什么屬性都沒有的,如果你傳進來一個“alert”,那么就是this.handlers[“alert”],即this.handlers.alert,?恩,this.handlers是空的,那么this.handlers還沒有alert這個屬性呢,所以他肯定是undefiend,if判斷為true,那么就給alert的這個屬性賦值一個空數組。handler是你傳進來的那個回調函數,所以,數組的第一個元素就是你傳進來的那個回調函數,下一次,你再在客戶端寫一下?win.on("alert",function(){alert("the second you click the alert button");});這時候alert這個屬性已經有了,就會把你傳進來的那個handler直接push進來。老師寫的這段代碼還是有點難懂,仔細再看看吧。
2016-03-27
還有重點想再問問什么
要進行這個判斷。。。