对象、它们的实例和 setTimeOut 效果

发布于 2024-11-07 04:47:43 字数 1906 浏览 3 评论 0原文

所以我正在尝试学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七堇年 2024-11-14 04:47:44

首先,nAntiFreezesetTimeout返回的原始值。您将该值传递回 clearTimtout,以便它知道要清除哪个超时。调用 clearTimeout 不会影响 nAntiFreeze 的值。

其次,that.loadPage未定义,因为that在调用doStock()时引用this(在调用它的地方)作为带有 new 的构造函数,它引用一个新对象)。但是您的函数不会返回该对象(即构造函数的 this),它会在 return 之后返回 loadPage() 函数所在的对象的一种方法.换句话说,您引用了错误的对象。

当您调用 oStocker.loadPage() 时,其 this 关键字引用 oStocker 对象,但传递给 setTimeout 的函数引用 that,它有一个构造函数的 this 闭包。

以下内容应该可行:

loadPage : function() {

    // Declare that here
    var that = this;

    if (this.sLink.length == 0) return;
    if (this.oList.length == 0) return;

    // If called as oStocker.loadPage(), this (and that) is
    // the oStocker object.
    nAntiFreeze = setTimeout(function(){GM_log(that); that.loadPage();},30000);
    GM_log('antifreeze ' + nAntiFreeze);
    getPageAsync2(this.sLink,false,getContent);  //GM_xmlhttprequest
}

使用不返回其 this 的构造函数没有多大意义,您可以使用 Richard Cornford 的模块模式并使用闭包进行继承。

Firstly, nAntiFreeze is a primitive value returned by setTimeout. You pass that value back to clearTimtout so it knows which timeout to clear. Calling clearTimeout doesn't affect the value of nAntiFreeze.

Secondly, that.loadPage is undefined because that references this when doStock() is called (where it is called as a constructor with new, it references a new Object). But your function doesn't return that object (i.e. the constructor's this), it returns the object after return that the loadPage() function is a method of. In other words, you are referencing the wrong object.

When you call oStocker.loadPage(), its this keyword references the oStocker object, but the function passed to setTimeout is referencing that, which has a closure to the constructor's this.

The following should work:

loadPage : function() {

    // Declare that here
    var that = this;

    if (this.sLink.length == 0) return;
    if (this.oList.length == 0) return;

    // If called as oStocker.loadPage(), this (and that) is
    // the oStocker object.
    nAntiFreeze = setTimeout(function(){GM_log(that); that.loadPage();},30000);
    GM_log('antifreeze ' + nAntiFreeze);
    getPageAsync2(this.sLink,false,getContent);  //GM_xmlhttprequest
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文