Javascript 在原型函数中访问原型函数
我试图调用原型中定义的登录函数,但调用函数(刷新)也在原型中Javascript 在原型函数中访问原型函数,javascript,function,prototype,prototype-programming,Javascript,Function,Prototype,Prototype Programming,我试图调用原型中定义的登录函数,但调用函数(刷新)也在原型中 function Checker() { var self = this; self.refresh(); window.setInterval(function(){self.refresh()}, 1000); } Checker.prototype = { refresh: function() { if(some condition)
function Checker() {
var self = this;
self.refresh();
window.setInterval(function(){self.refresh()}, 1000);
}
Checker.prototype = {
refresh: function() {
if(some condition){
login(); // this won't work, neither self.login();
}
},
login: function() {
}
};
如何在刷新函数中调用登录函数?从原型方法,实例设置为
this
。因此,this.login()
就是您想要的。通过原型方法,实例被设置为this
。所以this.login()
就是你想要的。很简单:使用this.login()很简单:使用this.login()
明白了,谢谢@巴马戈特,谢谢@巴尔马