2015-04-16 65 views
0

我正在尝试向我的作用域添加事件处理程序。功能addEvent工作正常。但是,如果我通过一个函数来处理程序是这样的:如何传递封闭范围中的事件处理程序

this.addEvent(window, "resize", self.resizeAd(this)); 

的功能将不会被传递给事件(The eventHandle is undefinded),我不知道为什么。

这里是我的代码:

(function(window, document, undefined) { 
    'use strict'; 
    var adTech = window.adTech = { 
     get: function() { 
      return _instance; 
     }, 
     //Main entry point. 
     init: function(options) { 
      return _instance || new ADTECH(options); 
     } 
    }; 
    var ADTECH = function(options) { 
     var defaultOptions = { 
      adID : '5202402', 
      hiddenClassName : 'hidden' 
     }; 
     this.options = this.extend(options, defaultOptions); 
     this.makeAd(); 
     _instance = this; 
     return _instance; 
    } 

    ADTECH.prototype = { 
     extend: function(source, target) { 
      if (source == null) { return target; } 
      for (var k in source) { 
       if(source[k] != null && target[k] !== source[k]) { 
        target[k] = source[k]; 
       } 
      } 
      return target; 
     }, 
     log: function(msg){ 
      if(window.console){ 
       console.log(msg); 
      } 
     }, 
     debounce: function(func, wait, immediate) { 
      var timeout; 
      return function() { 
       var context = this, args = arguments; 
       var later = function() { 
        timeout = null; 
        if (!immediate) func.apply(context, args); 
       }; 
       var callNow = immediate && !timeout; 
       clearTimeout(timeout); 
       timeout = setTimeout(later, wait); 
       if (callNow) func.apply(context, args); 
      }; 
     }, 
     addEvent: function (elem, type, eventHandle) { 
      console.log("adEvent is undefined: ",eventHandle); 
      if (elem == null || typeof(elem) == 'undefined') return; 
      if (elem.addEventListener) { 
       elem.addEventListener(type, eventHandle, false); 
      } else if (elem.attachEvent) { 
       elem.attachEvent("on" + type, eventHandle); 
      } else { 
       elem["on" + type] = eventHandle; 
      } 
     }, 
     // Function to resize div ad to take the full space we need/get 
     resizeAd : function(this){ 
      this.log("resizeAd done."); 
      var ad = document.getElementById(this.options.adID); 
      this.log(ad); 
      // do not work 
      // this.debounce(function() { 
      // console.log("tat:"); 
      // }, 250) 
     }, 
     // insert ad 
     makeAd: function() { 
      this.addEvent(window, "resize", this.resizeAd(this)); 
      var self = this; 
      //also do not work 
      this.addEvent(window, "resize", self.resizeAd(this)); 
     } 
    } 
    // Singleton 
    var _instance; 
}(window, document)); 

var API = adTech.init(); 
+0

'this.resizeAd(this)'是一个**函数调用** - 你没有传递函数,你传递调用函数的结果。 – Pointy

+0

好吧我把它推到this.addEvent(窗口,“resize”,this.resizeAd); 但现在我没有进一步访问此(例如this.log) – hamburger

回答

0

修改resizeAd函数返回的功能。在这种情况下,也不需要将对象显式传递给方法。另外this是保留字,不能用作参数。

// ... 
resizeAd: function(invokeLater) { 
    // this is explicitly set based on the calling object. 
    var obj = this; 
    var helper = function() 
     obj.log("resizeAd done."); 
     var ad = document.getElementById(obj.options.adID); 
     obj.log(ad); 
     obj.debounce(function() { 
      console.log("tat:"); 
     }, 250) 
    }; 
    // if invokeLater is a falsey value do the resizing right away 
    // if it is truthy return helper so that it can be assigned as 
    // an event handler 
    return invokeLater ? helper : helper(); 
}, 
makeAd: function() { 
    this.addEvent(window, "resize", this.resizeAd(true)); 
} 

// note that you can still call resize normally. 
this.resize(); 

你是因为你是分配事件处理程序调用该函数获取The eventHandle is undefinded错误。

this.addEvent(window, "resize", this.resizeAd(this));

JavaScript函数隐含返回undefined时return没有明确声明。除了在函数调用new的情况下,在这种情况下,新创建的对象被隐式返回。

+0

thx它似乎工作。现在我有一个问题,那就是debounce-funktion在没有任何错误的情况下无法正常工作。你有提示吗? – hamburger

+0

是的,所以我猜'debounce'在某个时间点通过了'resizeAd'。尝试我刚才编辑的内容,它们可能会有所帮助。 – robbmj

+0

debounce shout pass resizeAd debouncing。请看代码顶部 – hamburger

相关问题