标签:style blog color io ar for div cti log
1 ;(function(global ,undefined){ 2 var evts = {} 3 ,onceTag = ‘__event_once‘ 4 function emit(event ){ 5 var args = util.toArray(arguments , 1) 6 if (!(event in evts)) return 7 var _dels = [] 8 for (var i = 0 , j = evts[event].length ; i < j ;i ++){ 9 var cbk = evts[event][i] 10 if (!cbk) return 11 cbk.apply(null , args) 12 if (cbk[onceTag]) { evts[event][i] = null ; _dels.push[i]} 13 } 14 for (var i = _dels.length -1 ; i>=0 ; i--) evts[event].splice(_dels[i] , 1) 15 } 16 17 function addMultiCon(event , listener){ 18 var once = true 19 event.sort() 20 addListener(event.join(‘|‘) , listener , once) 21 var eventBubbles = [] 22 function tinOpener(evt){ 23 eventBubbles.push(evt) 24 if (eventBubbles.length >= event.length) { 25 eventBubbles.sort() 26 emit(eventBubbles.join(‘|‘)) 27 } 28 } 29 30 for (var i = 0 ; i < event.length;i ++){ 31 addListener(event[i] , tinOpener.bind(null, event[i]) , once) 32 } 33 } 34 35 function addListener(event , listener , once){ 36 if (util.isArray(event)) return addMultiCon(event , listener) 37 38 39 if (!(event in evts)) evts[event] = [] 40 if (once) listener[onceTag] = true 41 evts[event].push(listener) 42 } 43 44 function removeListener(event, listener){ 45 if (!listener) { 46 delete evts[event] 47 return 48 } 49 for (var i = 0 , j = evts[event].length ; i < j ;i ++){ 50 if (evts[event][i] === listener) { evts[event].splice(i, 1) ; break} 51 } 52 } 53 54 function listeners(event){ 55 return evts[event] 56 } 57 global.emitter = { 58 on : addListener 59 ,once : function(event , listener){ 60 addListener(event , listener , true) 61 } 62 ,emit : emit 63 ,listeners : listeners 64 } 65 })(this)
function toArray(colletions ,offset){ return Array.prototype.slice.call(colletions , offset || 0) }
emitter.on([‘test‘,‘test2‘] , function(){
console.log(‘test is fired‘)
})
emitter.emit(‘test‘)
emitter.emit(‘test2‘)
标签:style blog color io ar for div cti log
原文地址:http://www.cnblogs.com/vaal-water/p/3949359.html