开发者

JavaScript complains about a function I have defined

I am new to JavaScript and I'm trying to write a simple object that calls a few member functions.

Surprisingly, JavaScript complains about a function called uninstallLocalHost.

Error: uninstallLocalHost is not defined
Source File: chrome://custombutton/content/button.js
Line: 39

Yet, it looks like this function is defined. What could I be doing wrong?

var katimbaClass=
{       
    installLocalHost:function()
    {
        alert("localhost installed");
    },

    uninstallLocalHost:function()
    {
     开发者_StackOverflow社区   alert("localhost uninstalled");
    },

    toggleInstall:function()
    {
        if(bInstalled) uninstallLocalHost();
         else installLocalHost();
    },

    bInstalled: false
};

When I attempt to call a function of katimbaClass elsewhere like so:

oncommand="katimbaClass.toggleInstall()"

I don't understand why the following error results:

Error: uninstallLocalHost is not defined


In JavaScript, this is not implicit. You must change these lines:

if(bInstalled) uninstallLocalHost();
else installLocalHost();

To these:

if(this.bInstalled) this.uninstallLocalHost();
else this.installLocalHost();

...or, alternatively, these:

if(katimbaClass.bInstalled) katimbaClass.uninstallLocalHost();
else katimbaClass.installLocalHost();


Does this makes it simpler ?

var katimbaClass = new function() {
    this.installLocalHost= function () {
        alert("localhost installed");    
    };
    this.uninstallLocalHost= function () {
        alert("localhost UnInstalled");    
    };
    // ETC ..

}


oncommand="katimbaClass.toggleInstall()"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜