简体   繁体   English

如何在JavaScript中创建全局变量?

[英]How do I make a Global Variable in JavaScript?

So I would like for dashboard to be able to be modified inside one function, then be displayed in another function. 因此,我希望仪表板能够在一个功能中进行修改,然后在另一个功能中显示。 Kind of like a public variable in java. 有点像java中的公共变量。 Is this possible? 这可能吗? See my code below. 请参阅下面的代码。

var dashboard = new Array();
function init() {
    getXML(); //1.  goto get XML 2.// XML Parser
    displayXML();
}

function getXML() {
    console.log("getXML REACHED");
    $.ajax({
        type: "GET",
        url: "file:///H:/ModelDisplayV1/ModelDisplayV1/files/dashboard.xml",
        dataType: "xml",
        success: xmlParser
    });
}

function xmlParser(xml) {
    dashboard[0] = 7;
    console.log(dashboard);
});
}

function displayXML() {
    console.log("display xml function reached!!!");
    console.log(dashboard);
}

When I finally try and get the console.log(dashboard) it says dashboard is undefined . 当我最终尝试获取console.log(dashboard)它说dashboard is undefined I thought by declaring dashboard outside of my functions it would be global. 我认为,通过在功能之外声明仪表板将是全局的。 How do I make it so I can alter the contents of dashboard in one function and retrieve them in another function? 如何做到这一点,以便可以在一个功能中更改仪表板的内容并在另一个功能中进行检索?

I am more familiar with Java as opposed to Javascript. 我比Java更熟悉Java。

The ajax call is asynchrous, so the displayXML() function is called in the init() method before dashboard is actually filled. ajax调用是异步的,因此在实际填充仪表板之前,请在init()方法中调用displayXML()函数。 So do this instead: 因此改为:

var dashboard = new Array();
function init() {
    getXML(); //1.  goto get XML 2.// XML Parser
}

function getXML() {
    console.log("getXML REACHED");
    $.ajax({
        type: "GET",
        url: "file:///H:/ModelDisplayV1/ModelDisplayV1/files/dashboard.xml",
        dataType: "xml",
        success: xmlParser
    });
}

function xmlParser(xml) {
    dashboard[0] = 7;
    console.log(dashboard);
    displayXML();
});
}

function displayXML() {
    console.log("display xml function reached!!!");
    console.log(dashboard);
}

There's only two kinds of scopes in javascript - global and function. javascript中只有两种作用域-全局作用域和函数。 If it's not declared inside a function, it's global. 如果未在函数内部声明,则为全局。 If it's not declared using var, it's also implicitly global. 如果未使用var声明,则它也是隐式全局的。

The var keyword declares a variable as local, otherwise you can omit it and it will make it global. var关键字将变量声明为局部变量,否则您可以忽略该变量并将其全局化。

The other option is to insert it into the window object like so: 另一个选择是将其插入窗口对象,如下所示:

window.dashboard = new Array();

This is the preferred method for insert variables into the global scope. 这是将变量插入全局范围的首选方法。

Also blah blah blah about not abusing global variables that you probably know. 同样,不要滥用您可能知道的全局变量。

if you want Java-like class semantics in javascript look at using the Revealing Module pattern. 如果您想在javascript中使用类似Java的类语义,请使用Revealing Module模式查看。

http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript

Getting in the habit of correctly organizing your javascript code from the beginning will save you many headaches. 从一开始就养成正确组织JavaScript代码的习惯,将为您省去很多麻烦。

You need to pass a context to $.ajax() 您需要将context传递给$.ajax()

$.ajax({
    type: "GET",
    url: "file:///H:/ModelDisplayV1/ModelDisplayV1/files/dashboard.xml",
    dataType: "xml",
    context: this,
    success: xmlParser 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM