[英]Correctly set global variable from JavaScript
I have the simple code: 我有简单的代码:
<script type="text/javascript">
var dataFromBrowser;
var dataForStore = [];
var callServerOwnerId = {
callback:callbackFunction,
arg: dataFromBrowser
};
// call to DWR function - from Java
AssetScreener.getEntityOwnerIds(callServerOwnerId);
function callbackFunction(dataFromServer, arg1) {
// yes, I see what I need
alert(dataFromServer);
return dataForStore[0] = dataFromServer[0];
}
console.log(dataForStore);
The problem is that I need to retrieve data from my callbackFunction
and set data to dataForStore
? 问题是我需要从我的
callbackFunction
检索数据并将数据设置为dataForStore
?
In javascript setting a global variable is as simple as ommitting the var keyword. 在javascript中,设置全局变量就像省略var关键字一样简单。
For example: 例如:
var someVar = 5
function foo(){
someVar = someVar + 1;
}
Will produce undefined while 将产生undefined
someVar = 5;
function foo(){
someVar = someVar+1;
}
Will produce 6. Note that generally speaking (there are of course exceptions), if you're using global variables you're doing it wrong. 将产生6。请注意,通常来说(当然会有例外),如果您使用全局变量,那么您做错了。
在dataForStore之前删除var,它将成为全局变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.