[英]Javascript Global Variable is empty
I have a function and created a global variable. 我有一个函数,并创建了一个全局变量。
The alert inside the function is alerting the result as expected but the variable is showing nothing. 函数内部的警报正在按预期警报结果,但变量未显示任何内容。
How can I fix this? 我怎样才能解决这个问题?
Here's the code: 这是代码:
var connectionResult = '';
function checkConnection() {
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'No network connection';
alert('Connection type: ' + states[networkState]);
var connectionResult = states[networkState];
};
checkConnection();
alert(connectionResult); // Returns Nothing
The problem is that you are creating a local variable named connectionResult in the checkConnection rather than assigning to the global connectionResult. 问题是您要在checkConnection中创建一个名为connectionResult的局部变量,而不是分配给全局connectionResult。
Replace 更换
var connectionResult = states[networkState];
with 同
connectionResult = states[networkState];
And it will work. 它将起作用。
To expand on TJ Crowder's comment below, you can make this function a little more efficient, since you are declaring what is essentially a constant over and over again. 为了进一步扩展下面的TJ Crowder的评论,您可以使此函数更有效率,因为您一遍又一遍地声明了什么是常量。 You can change the code as follows:
您可以按以下方式更改代码:
var NetworkStates = {}; // this never changed in the old function, so refactored it out as a "constant"
NetworkStates[Connection.UNKNOWN] = 'Unknown connection';
NetworkStates[Connection.ETHERNET] = 'Ethernet connection';
NetworkStates[Connection.WIFI] = 'WiFi connection';
NetworkStates[Connection.CELL_2G] = 'Cell 2G connection';
NetworkStates[Connection.CELL_3G] = 'Cell 3G connection';
NetworkStates[Connection.CELL_4G] = 'Cell 4G connection';
NetworkStates[Connection.CELL] = 'Cell generic connection';
NetworkStates[Connection.NONE] = 'No network connection';
function getConnectionState() {
return NetworkStates[navigator.connection.type];
}
Now wherever you need the connection state you can just call getConnectionState rather than having a global variable floating around. 现在,无论您在何处需要连接状态,都可以调用getConnectionState而不是使用全局变量。
var connectionResult
inside checkConnection
creates a new variable called connectionResult
. checkConnection
内部的var connectionResult
创建一个名为connectionResult
的新变量。
This "inner" variable is only in scope inside checkConnection
. 此“内部”变量仅在
checkConnection
范围内。 It hides, or "shadows" the one that you intend to use: any reference to connectionResult
inside checkConnection
uses it instead of the "outer" variable that you expect. 它隐藏或“遮盖”了您打算使用的那个:
checkConnection
内部对connectionResult
任何引用都使用它,而不是您期望的“外部”变量。
Just remove var
and you'll use the existing connectResult
: 只需删除
var
,就可以使用现有的connectResult
:
connectionResult = states[networkState];
var connectionResult = states[networkState];
creates a new variable connectionResult
within the scope of the function which is completly unrelated to the global variable connectionResult
创建一个新的变量
connectionResult
功能的范围内其是完全地无关的全局变量connectionResult
Just use 只需使用
connectionResult = states[networkState];
in order to assign the network state to the global variable 为了将网络状态分配给全局变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.