[英]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. 我是JavaScript的新手,正在尝试编写一个简单的对象,该对象调用一些成员函数。
Surprisingly, JavaScript complains about a function called uninstallLocalHost
. 令人惊讶的是,JavaScript抱怨称有一个名为
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()
{
alert("localhost uninstalled");
},
toggleInstall:function()
{
if(bInstalled) uninstallLocalHost();
else installLocalHost();
},
bInstalled: false
};
When I attempt to call a function of katimbaClass
elsewhere like so: 当我尝试在其他地方调用
katimbaClass
的函数时, katimbaClass
所示:
oncommand="katimbaClass.toggleInstall()"
I don't understand why the following error results: 我不明白为什么会出现以下错误:
Error: uninstallLocalHost is not defined
In JavaScript, this
is not implicit. 在JavaScript中,
this
不是隐式的。 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()"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.