Javascript错误,无法识别函数.为什么

Javascript error, not recognizing function... why?

本文关键字:识别 函数 为什么 错误 Javascript      更新时间:2023-09-26

这是我的代码(在firefox插件中)

this.something_something = function (the_actual_url) {
    this.this_version = null;
    try {
        // Firefox 4 and later; Mozilla 2 and later
        Components.utils.import("resource://gre/modules/AddonManager.jsm");
        AddonManager.getAddonByID("parasites@maafire.com", function (addon) {
            this_version = addon.version;
        });
    }
    catch (ex) {
        // Firefox 3.6 and before; Mozilla 1.9.2 and before
        var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
        var addon = em.getItemForID("parasites@maafire.com");
        this_version = addon.version;
    }

    this.timervar = setTimeout(function () {
        this.get_plugin_version(this.this_version);
    }, 2000);
}
this.get_plugin_version = function (blah) {
    alert("aa:" + blah);
}

我得到错误:

错误:这个。Get_plugin_version不是一个函数源文件:chrome://mf_monkey_project/content/overlay.js行:476

我做错了什么?

对不起,我把格式搞砸了,但是我删除了大量的代码来适应这里,这使得格式都很混乱。

因为setTimeout回调将在全局上下文中执行。

你可以使用bind() [docs]方法来绑定回调所需的上下文和参数。

this.timervar=setTimeout( this.get_plugin_version.bind(this, this.this_version),
                          2000 );

如果您不希望this.this_version的当前值永久绑定为第一个参数,则从.bind()调用中删除它。