1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| let Observer = (function(){ let t__messages = {}; return { regist: function(type, fn) { if(typeof __messages[type] === 'undefined') { messages[type] = [fn]; }else { __messages[type].push(fn); } }, fire: function(type, args) { if(!__messages[type]){ return } let events = { type: type, args: args || {} }, i = 0, len = __messages[type].length; for(;i<len;i++){ __messages[type][i].call(this, events); } }, remove: function(type, fn) { if(__messages[type] instanceof Array){ let i = __messages[type].length -1; for(;i>=0;i--){ __messages[type][i] === fn && __messages[type].splice(i, 1) } } } } })();
|