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()"
精彩评论