对象、它们的实例和 setTimeOut 效果
所以我正在尝试学习 JavaScript 中的面向对象编程。
function doStock() { //my class
var that = this;
var nAntiFreeze = null; // timeout ID
var getContent = function(oInPageContainer) {
GM_log('Antifreeze, before clear ' +nAntiFreeze);
//clearTimeout(nAntiFreeze);
GM_log('Antifreeze, after clear ' +nAntiFreeze);
};
return {
sLink : "",
oList : "",
sSplitOperator : ";",
reset : function() {
this.sLink = '';
this.oList = '';
this.sSplitOperator = ';';
nAntiFreeze = null;
},
loadPage : function() {
if (this.sLink.length == 0) return;
if (this.oList.length == 0) return;
nAntiFreeze = setTimeout(function(){GM_log(that); that.loadPage();},30000);
GM_log('antifreeze ' + nAntiFreeze);
getPageAsync2(this.sLink,false,getContent); //GM_xmlhttprequest
}
}
};
我的脚本在 FireFox 4 中的 GreaseMonkey 上运行。在我的代码中,我使用上面的函数/类来创建一个对象,如下所示。
var oStocker = new doStock();
oStocker.sLink = 'www.somepage.com';
oStocker.oList = 'some list, may be a string line or the array object';
oStocker.loadPage();
getPageAsync2
函数调用 GM_xmlhttprequest,然后将 div 容器内的结果页面内容返回给回调函数。
首先,一般问题:在我调用 clearTimeOut
函数后,nAntiFreeze 的值没有重置为 null 或任何其他值。这是正常的吗?
第二个问题:为什么当超时时,我收到错误 that.loadPage() is not a function
? GM_log(that) 告诉我[object Object]。
这个问题上的一个人能够通过使用 var that = this 来使其工作。但为什么它对我不起作用? 带有 setTimeout 的自定义对象调用方法失去范围
编辑:第三个问题:如果我创建一百万个对象会发生什么。当它们完成工作后,浏览器会删除它们吗?因为我确实无法释放它们,因为该对象使用异步 ajax 调用,这意味着我无法执行
var oStocker = new doStock();
oStocker.loadPage();
oStocker = null;
oStocker = null 将在我的对象完成工作之前被调用。
谢谢
So i am trying to learn object oriented programming in javascript.
function doStock() { //my class
var that = this;
var nAntiFreeze = null; // timeout ID
var getContent = function(oInPageContainer) {
GM_log('Antifreeze, before clear ' +nAntiFreeze);
//clearTimeout(nAntiFreeze);
GM_log('Antifreeze, after clear ' +nAntiFreeze);
};
return {
sLink : "",
oList : "",
sSplitOperator : ";",
reset : function() {
this.sLink = '';
this.oList = '';
this.sSplitOperator = ';';
nAntiFreeze = null;
},
loadPage : function() {
if (this.sLink.length == 0) return;
if (this.oList.length == 0) return;
nAntiFreeze = setTimeout(function(){GM_log(that); that.loadPage();},30000);
GM_log('antifreeze ' + nAntiFreeze);
getPageAsync2(this.sLink,false,getContent); //GM_xmlhttprequest
}
}
};
My script runs on GreaseMonkey in FireFox 4. In my code i am using the above function/class to make an object as follows.
var oStocker = new doStock();
oStocker.sLink = 'www.somepage.com';
oStocker.oList = 'some list, may be a string line or the array object';
oStocker.loadPage();
getPageAsync2
function calls GM_xmlhttprequest and then returns the result page contents inside a div container to the callback function.
First, general question: value of nAntiFreeze does not get reset to null or anything after i call clearTimeOut
function. Is this normal?
Second question: why when the timeout runs out, i get the error that.loadPage() is not a function
? GM_log(that) tells me [object Object].
A person on this question was able to make it work by using var that = this
. But why is it not working for me?
Custom Object calling Methods with setTimeout loses scope
EDIT: Third question: What happens if i create a million objects. Will browser get rid of them when they are done working? Because i sure am unable to free them as this object uses asynchronous ajax calls, which means that i can't do
var oStocker = new doStock();
oStocker.loadPage();
oStocker = null;
The oStocker = null will be called before my object even finished working.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
nAntiFreeze
是setTimeout
返回的原始值。您将该值传递回clearTimtout
,以便它知道要清除哪个超时。调用clearTimeout
不会影响nAntiFreeze
的值。其次,
that.loadPage
未定义,因为that
在调用doStock()
时引用this
(在调用它的地方)作为带有new
的构造函数,它引用一个新对象)。但是您的函数不会返回该对象(即构造函数的this
),它会在return
之后返回loadPage()
函数所在的对象的一种方法.换句话说,您引用了错误的对象。当您调用
oStocker.loadPage()
时,其this
关键字引用oStocker
对象,但传递给 setTimeout 的函数引用that
,它有一个构造函数的this
闭包。以下内容应该可行:
使用不返回其
this
的构造函数没有多大意义,您可以使用 Richard Cornford 的模块模式并使用闭包进行继承。Firstly,
nAntiFreeze
is a primitive value returned bysetTimeout
. You pass that value back toclearTimtout
so it knows which timeout to clear. CallingclearTimeout
doesn't affect the value ofnAntiFreeze
.Secondly,
that.loadPage
is undefined becausethat
referencesthis
whendoStock()
is called (where it is called as a constructor withnew
, it references a new Object). But your function doesn't return that object (i.e. the constructor'sthis
), it returns the object afterreturn
that theloadPage()
function is a method of. In other words, you are referencing the wrong object.When you call
oStocker.loadPage()
, itsthis
keyword references theoStocker
object, but the function passed to setTimeout is referencingthat
, which has a closure to the constructor'sthis
.The following should work:
There isn't much point using a constructor that doesn't return its
this
, you can use Richard Cornford's module pattern and use closures for inheritance.