如何引用';雷亚尔';这是在使用JavaScript OOP的附加事件处理程序中实现的吗?
如何使用JavaScript OOP在附加的事件处理程序中引用此“真实”事件 使用局部变量“anyVariable”引用实例的最佳实践是什么如何引用';雷亚尔';这是在使用JavaScript OOP的附加事件处理程序中实现的吗?,javascript,oop,Javascript,Oop,如何使用JavaScript OOP在附加的事件处理程序中引用此“真实”事件 使用局部变量“anyVariable”引用实例的最佳实践是什么 var myClass = (function () { function myClass(gridId) { $(function () { // misc init code replaced here this.initKeyboard(); }); } myClass.prototype.init
var myClass = (function () {
function myClass(gridId) {
$(function () {
// misc init code replaced here
this.initKeyboard();
});
}
myClass.prototype.initKeyboard = function () {
var anyVariable = this; // keyword 'this' is clearly referring here to the instance
$(document).keyup(function (e) {
// I would like to refer here to the instance.
// However it seems that the keyword 'this' refers here to the function (?)
// Is is the best practice here to refer to the instance by using the local variable 'anyVariable'?
anyVariable.myMember(...); //???
}
});
};
通常的变量名是
self
,that
,或\u this
(有关更多详细信息,请参见此),但这是在上下文更改时保持所需this
值的常用方法。此外,您可以使用$.proxy