可能重复:
“this” keyword in event methods when using JavaScript prototype object
我创建了一个名为" MyClass"在javascript中并将onclick事件绑定到页面上的按钮。当onclick事件触发时我想调用另一个成员函数,但它给了我未定义的错误。请帮忙。
//MyClass constructor
MyClass = function() {
//Find search button
this.ctrlSearchButton = $("#btnSearch");
//Attach onclick event to search button
this.ctrlSearchButton.click(this.onSearchButtonClick);
};
MyClass.prototype.onSearchButtonClick = function () {
DoSearch();// ERROR : Object Expected
};
MyClass.prototype.DoSearch = function () {
alert("search called");
};
答案 0 :(得分:0)
MyClass = function() {
var self = this;
this.onSearchButtonClick = function () {
self.DoSearch();// ERROR : Object Expected
};
this.DoSearch = function () {
alert("search called");
};
....
}