比如一个简单的函数
function computer(obj) {
  return function(){ …… }
}this.funS = computer(obj);当用在注册事件addEventListener,removeEventListener时,用funS没有问题。而直接用computer(obj) 不会报错,但响应后无法解除?
求解其中的区别?谢谢!

解决方案 »

  1.   

    funS的函数指针只有一个,但调用computer(obj)每次返回的函数指针都不同,肯定不能解除。
      

  2.   

    那么若函数computer(obj)中不用return,像这样
    function computer(obj) {
       …… 
    }
    对解除事件是否有影响呢?非常感谢!
      

  3.   

    那computer的函数指针就确定了,注册解除都没有问题
      

  4.   

    这样能把当前响应事件解除了,但现在有多个已经注册的事件放在不同的类中,除了一步一步remove外有没有其他办法?求好的解决方法?非常感谢!
      

  5.   

    可以用jquery的事件命名空间,或者自己实现吧。。    <input id="txt" type="text"/>
        <script>
         function addEventListener(elem, type, group, callback){
         var args = arguments;
    if(args.length < 3 && elem instanceof HTMLElement && typeof type !== "string" && typeof group !== "string" && typeof group !== "function"){
    throw "arguments error";
    }else if(typeof group === "function" && typeof callback === "undefined"){
    callback = group;
    group = "default";
    }
    if(!elem["handler"]){
    elem["handler"] = {};
    }
    if(!elem["handler"][type]){
    elem["handler"][type] = {};
    }
    if(!elem["handler"][type][group]){
    elem["handler"][type][group] = [];
    }
    elem["handler"][type][group].push(callback);
    elem.addEventListener(type, callback, false);
         }
         function removeEventListener(elem, type, group, callback){
         var arr, args = arguments;
    if(args.length === 0 && elem instanceof HTMLElement && typeof type !== "string" && typeof group !== "string" && typeof group !== "function"){
    throw "arguments error";
    }
    if(!elem["handler"]){
    throw "never add event.";
    }else if(!elem["handler"][type]){
    throw ("never add " + type + "event.");
    }else if(!elem["handler"][type][group]){
    throw (group + "isn\'t exist.");
    }

    if(args.length === 2){
    arr = elem["handler"][type];
    for(var i in arr){
    for(var j in arr[i]){
    elem.removeEventListener(type, arr[i][j], false);
    }
    }
    }else if(args.length === 3 && typeof group === "string"){
    arr = elem["handler"][type][group];
    for(var i in arr){
    elem.removeEventListener(type, arr[i], false);
    }
    }else{
    if(typeof group === "function"){
    callback = group;
    group = "default";
    }
    arr = elem["handle"][type][group];
    for(var i = 0, len = arr.length; i < len; i++){
    if(callback === arr[i]){
    arr.splice(i, 1);
    elem.removeEventListener(type, arr[i], false);
    }
    }
    }
         }
         var txt = document.getElementById("txt");
         addEventListener(txt, "click", "a", function(){
         alert("a1");
         });
         addEventListener(txt, "click", "a", function(){
         alert("a2");
         });
         addEventListener(txt, "click", "b", function(){
         alert("b1");
         });
        
         addEventListener(document, "dblclick", function(){
         removeEventListener(txt, "click", "a");
         });
        </script>