【发布时间】:2014-08-18 01:02:08
【问题描述】:
我正在尝试将参数传递给class 方法,但是当我将该方法与事件绑定时,它仅在启动类时工作一次。
但是当我尝试不传递参数时,它会像往常一样工作,并且可以正确处理事件。
我无法在类内为displayMessageWithParam($param) 设置参数,因为它可能被其他人使用,我希望他们传递他们的参数。它可以在没有绑定到事件的情况下工作,但是当我绑定到事件时为什么不呢?
请告诉我我做错了什么:
jQuery(function() {
// create document
Klass.site = new Klass.site();
// need to call init manually with jQuery
Klass.site.initialize();
});
// namespace our code
window.Klass = {};
// my class (no Class.create in JQuery so code in native JS)
Klass.site = function() {};
// add functions to the prototype
Klass.site.prototype = {
// automatically called
initialize: function() {
// this works as usual
jQuery('#field').keyup( jQuery.proxy(this.displayMessage, this));
// but this wont work
jQuery('#field').keyup( jQuery.proxy(this.displayMessageWithParam('chanaged'), this));
},
displayMessage:function(){
console.log('chanaged');
}
displayMessageWithParam:function($parm){
console.log($parm);
}
};
感谢您的帮助。
【问题讨论】:
-
不应该是
'changed'而不是'chnaged'?
标签: javascript jquery oop bind