[英]Assigning multiple global variables in JavaScript with one function
I've been looking around for a while and cant seem to find a solution to this question.我已经环顾了一段时间,似乎无法找到这个问题的解决方案。
I have three global variables declared in JavaScript, which haven't been assigned yet such as:我在 JavaScript 中声明了三个全局变量,尚未分配,例如:
var GLOBAL_VARIABLE_ONE;
var GLOBAL_VARIALLE_TWO;
var GLOBAL_VARIABLE_THREE;
Lets say I have a function which I pass a two parameters, one of them is a url string to make an ajax call to retrieve a JSON object, the other is the global variable which I want to assign the returned JSON to.假设我有一个函数,我传递了两个参数,其中一个是 url 字符串,用于进行 ajax 调用以检索 JSON 对象,另一个是我想将返回的 JSON 分配给的全局变量。 Thus, I have a function as such:因此,我有一个这样的功能:
function getBackList(urlName, globalVariable) {
$.ajax({
type: "GET",
url: urlName,
}).done(function (returned_data) {
globalVariable = $.parseJSON(returned_data);
});
}
I want to be able to call the function like:我希望能够像这样调用函数:
getBackList("\some\url", GLOBAL_VARIABLE_ONE);
and assign the object to the global.并将对象分配给全局。
Now, I realize that using global variables are not recommended and I understand that due to variable hoisting, the arguments passed to a function are locally scoped.现在,我意识到不推荐使用全局变量,并且我理解由于变量提升,传递给函数的参数是局部范围的。 However, I need to be able to access these global variables in other functions after they have been assigned.但是,在分配这些全局变量后,我需要能够在其他函数中访问这些全局变量。
My question is how can I pass the global variables and assign them into the one function above, without having to create separate functions to assign each one explicitly (which works by the way but creates a lot of code redundancy)?我的问题是如何传递全局变量并将它们分配给上面的一个函数,而不必创建单独的函数来显式分配每个函数(顺便说一下,这会产生很多代码冗余)?
function getBackList(urlName, globalVariableName) {
$.ajax({
type: "GET",
url: urlName,
}).done(function (returned_data) {
window[globalVariableName] = $.parseJSON(returned_data);
});
}
Pass in the global variable's name instead ^^^而是传入全局变量的名称 ^^^
global variables are actually members of the window
object.全局变量实际上是window
对象的成员。 So you could do:所以你可以这样做:
function getBackList(x, variableName) {
// .. stuff ..
window[variableName] = $.parseJSON(returned_data);
}
getBackList(x, 'GLOBAL_VARIABLE_ONE');
Javascript won't allow that as it's passing the variable. Javascript 不允许这样做,因为它正在传递变量。
If GLOBAL_VARIABLE_ONE
is an object it'll pass a reference.如果GLOBAL_VARIABLE_ONE
是一个对象,它将传递一个引用。
Examples:例子:
a = {}
a
now references the new object. a
现在引用新对象。
b = a;
b
now references the same object that a references. b
现在引用b
引用的同一对象。 Note that it does not reference a
.需要注意的是它不引用a
。
With a = {}; b = a
用a = {}; b = a
a = {}; b = a
, you get a = {}; b = a
,你得到
a
\
\
{ }
/
/
b
Then with a['one'] = {}
you get然后用a['one'] = {}
你得到
a
\
\
{ one: { } }
/
/
b
In order to achieve what you want you'll want GLOBAL_VARIABLE_ONE
to be an object.为了实现您想要的,您需要GLOBAL_VARIABLE_ONE
成为一个对象。
So you can do:所以你可以这样做:
var GLOBAL_OBJECT = {};
getBackList("\some\url", GLOBAL_OBJECT['GLOBAL_VARIABLE_ONE']);
Or you can do:或者你可以这样做:
var GLOBAL_VARIABLE_ONE = {}
var GLOBAL_VARIALLE_TWO = {}
var GLOBAL_VARIABLE_THREE = {}
function getBackList(urlName, globalVariable) {
$.ajax({
type: "GET",
url: urlName,
}).done(function (returned_data) {
globalVariable.json = $.parseJSON(returned_data);
});
}
getBackList("\some\url", GLOBAL_VARIABLE_ONE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.