简体   繁体   English

JavaScript抱怨我定义的函数

[英]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.

相关问题 浏览器上的 Webpack 抱怨需要未定义 - Webpack on browser complains about require not defined JSLint抱怨函数表达式名称空间语法 - JSLint complains about function expression namespace syntax DynamoDB / marshal 函数抱怨未定义的值 - DynamoDB / marshal function complains about an undefined value 我需要告诉jslint另一个函数中尚未定义的函数吗? - Do I need to tell jslint about functions that have not yet been defined inside another function? Javascript错误日志显示在单独的文件甚至同一文件中定义函数时未定义的函数 - Javascript error log says function not defined when I have defined it in a separate file and even the same file 我在 angular 8 中使用外部 javascript 文件。它显示未定义函数的错误。 但我已经定义了函数 - I am using external javascript file in angular 8. It showing an error that function is not defined. But i have defined the function 在调用之前是否必须定义Javascript函数? - Does a Javascript function have to be defined before calling it? TypeScript抱怨HTMLElement没有value属性 - TypeScript complains about HTMLElement do not have value property 我对javascript名称空间有一些疑问 - I have some questions about javascript namespace 我在Javascript中有关于Bubble Process的问题 - I have a question about Bubble Process in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM