js自定义事件

自定义事件的本质,创建一个对象,然后把事件的名字作为对象的一个属性,然后value是一个[],把此事件的所以回调都push进去。

写一个很基本的,没有把对象暴露出去的js的自定义事件。

 1 var event = (function(){
 2   var obj = {};
 3   var addEvent = function(type,cb){
 4     if(!obj[type]){
 5         obj[type] = [];
 6     }
 7     return obj[type].push(cb);
 8   }
 9   var removeEvent = function(type){
10     return obj[type] = null;
11   }
12   var fireEvent = function(type){
13     for(var i = 0;i<obj[type].length;i++){
14       obj[type][i]();
15     }
16   }
17   return {
18     add:addEvent,
19     remove:removeEvent,
20     fire:fireEvent
21   }
22 })();
23 var on = function(type,param){
24   if(typeof param == "function"){
25     event.add(type,param);
26   }else{
27     event.fire(type);
28   }
29 }
30 var off = function(type){
31   event().remove(type);
32 }
33 on("hello",function(){console.log("你好世界");});
34 on("hello",function(){console.log("我是飘飘然");});
35 on("hello");

这里我们提供一个可以放入sdk中的

 1  customEvent = (function() {
 2       var S4, addCustomEvent, cgid, fireCustomEvent, guid, listeners, removeCustomEvent;
 3       S4 = function() {
 4         return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
 5       };
 6       guid = function() {
 7         return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4();
 8       };
 9       listeners = {};
10       cgid = '__ceGUID';//cgid = guid; 目的为了防止框架定义的obj属性和用户定义属性的相同
11       addCustomEvent = function(obj, event, callback) {
12         obj[cgid] = undefined;
13         if (!obj[cgid]) {
14           obj[cgid] = "ifvisible.object.event.identifier";
15         }
16         if (!listeners[obj[cgid]]) {
17           listeners[obj[cgid]] = {};
18         }
19         if (!listeners[obj[cgid]][event]) {
20           listeners[obj[cgid]][event] = [];
21         }
22         return listeners[obj[cgid]][event].push(callback);
23       };
24       fireCustomEvent = function(obj, event, memo) {
25         var ev, j, len, ref, results;
26         if (obj[cgid] && listeners[obj[cgid]] && listeners[obj[cgid]][event]) {
27           ref = listeners[obj[cgid]][event];
28           results = [];
29           for (j = 0, len = ref.length; j < len; j++) {
30             ev = ref[j];
31             results.push(ev(memo || {}));
32           }
33           return results;
34         }
35       };
36       removeCustomEvent = function(obj, event, callback) {
37         var cl, i, j, len, ref;
38         if (callback) {
39           if (obj[cgid] && listeners[obj[cgid]] && listeners[obj[cgid]][event]) {
40             ref = listeners[obj[cgid]][event];
41             for (i = j = 0, len = ref.length; j < len; i = ++j) {
42               cl = ref[i];
43               if (cl === callback) {
44                 listeners[obj[cgid]][event].splice(i, 1);
45                 return cl;
46               }
47             }
48           }
49         } else {
50           if (obj[cgid] && listeners[obj[cgid]] && listeners[obj[cgid]][event]) {
51             return delete listeners[obj[cgid]][event];
52           }
53         }
54       };
55       return {
56         add: addCustomEvent,
57         remove: removeCustomEvent,
58         fire: fireCustomEvent
59       };
60     })();
View Code
原文地址:https://www.cnblogs.com/coding4/p/6056775.html