此连接存在JavaScript作用域问题
我有下面的JavaScript代码。在函数更新中,此连接存在JavaScript作用域问题,javascript,scope,Javascript,Scope,我有下面的JavaScript代码。在函数更新中,this.connection解析为未定义,而不是数字。我做错了什么 function Net() { this.connection = -1; this.counter = 1; this.timelastsend = -1; setInterval( this.update, 3000); } Net.prototype.update = function() { if
this.connection
解析为未定义,而不是数字。我做错了什么
function Net()
{
this.connection = -1;
this.counter = 1;
this.timelastsend = -1;
setInterval( this.update, 3000);
}
Net.prototype.update = function()
{
if (this.connection > 0 && this.timelastsend > 0)
{
var now = new Date().valueOf();
if (now - this.timelastsend > 1000 * 60)
{
}
}
}
使用
this
的问题之一是this
取决于调用函数的方式
setInterval
将调用您的update
方法,就像它是一个独立的函数一样,因此此
将被设置为全局对象
如果确实需要使用this
功能,请按如下方式重写对setInterval的调用:
function Net() {
var self = this;
this.connection = -1;
this.counter = 1;
this.timelastsend = -1;
setInterval( function () { self.update() }, 3000);
}
function createNet() {
var connection = -1,
counter = -1,
timelastsent = -1,
self,
update;
update = function () {
var now;
if (connection > 0 && timelastsent > 0) {
now = new Date().valueOf();
if (now - timelastsent > 1000 * 60) {
// ... update code ...
counter += 1;
timelastsent = now;
}
}
};
setInterval(update, 3000);
return {
update: update,
getTimeLastSent: function () { return timelastsent; },
getCounter: function () { return counter; },
getConnection: function () { return connection; }
};
}
这样,您将创建一个self
变量,该变量将继续引用您的对象(如果您使用new
操作符创建了它,这是避免此
的另一个原因)
附录: 如果您没有主动从Net伪类中删除大量对象,我将按照如下方式重构:
function Net() {
var self = this;
this.connection = -1;
this.counter = 1;
this.timelastsend = -1;
setInterval( function () { self.update() }, 3000);
}
function createNet() {
var connection = -1,
counter = -1,
timelastsent = -1,
self,
update;
update = function () {
var now;
if (connection > 0 && timelastsent > 0) {
now = new Date().valueOf();
if (now - timelastsent > 1000 * 60) {
// ... update code ...
counter += 1;
timelastsent = now;
}
}
};
setInterval(update, 3000);
return {
update: update,
getTimeLastSent: function () { return timelastsent; },
getCounter: function () { return counter; },
getConnection: function () { return connection; }
};
}
你会注意到在任何地方都没有提到
这个
,这意味着没有歧义。我已经为connection、counter和timelastsent属性提供了三个getter,但是如果您希望这些getter可以从对象外部写入,您可以同样轻松地将它们添加到创建的对象中。使用this
的问题之一是this
依赖于调用函数的方式
setInterval
将调用您的update
方法,就像它是一个独立的函数一样,因此此
将被设置为全局对象
如果确实需要使用this
功能,请按如下方式重写对setInterval的调用:
function Net() {
var self = this;
this.connection = -1;
this.counter = 1;
this.timelastsend = -1;
setInterval( function () { self.update() }, 3000);
}
function createNet() {
var connection = -1,
counter = -1,
timelastsent = -1,
self,
update;
update = function () {
var now;
if (connection > 0 && timelastsent > 0) {
now = new Date().valueOf();
if (now - timelastsent > 1000 * 60) {
// ... update code ...
counter += 1;
timelastsent = now;
}
}
};
setInterval(update, 3000);
return {
update: update,
getTimeLastSent: function () { return timelastsent; },
getCounter: function () { return counter; },
getConnection: function () { return connection; }
};
}
这样,您将创建一个self
变量,该变量将继续引用您的对象(如果您使用new
操作符创建了它,这是避免此
的另一个原因)
附录: 如果您没有主动从Net伪类中删除大量对象,我将按照如下方式重构:
function Net() {
var self = this;
this.connection = -1;
this.counter = 1;
this.timelastsend = -1;
setInterval( function () { self.update() }, 3000);
}
function createNet() {
var connection = -1,
counter = -1,
timelastsent = -1,
self,
update;
update = function () {
var now;
if (connection > 0 && timelastsent > 0) {
now = new Date().valueOf();
if (now - timelastsent > 1000 * 60) {
// ... update code ...
counter += 1;
timelastsent = now;
}
}
};
setInterval(update, 3000);
return {
update: update,
getTimeLastSent: function () { return timelastsent; },
getCounter: function () { return counter; },
getConnection: function () { return connection; }
};
}
你会注意到在任何地方都没有提到
这个
,这意味着没有歧义。我已经为connection、counter和timelastsent属性提供了三个getter,但是如果您希望它们可以从对象外部写入,您可以同样轻松地将它们添加到创建的对象。这是否也会将.timelastsend解析为未定义?如何调用更新函数?如何实例化Net对象以及如何调用更新函数?就我所知,如果这是正确的,它应该可以工作。this.timelastsend是否也被解析为未定义?如何调用更新函数?如何实例化Net对象以及如何调用更新函数?据我所知,如果这是正确的,它应该工作。