admin管理员组文章数量:1332604
As per me, every variable declared in javascript above functions treat as global. But in my case, i unable to access that variable show undefined
. Here is jsfiddle
code:
__getUserByRmId('sun1')
var firstCallFlag = 1;
function __getUserByRmId(rmId) {
console.log(firstCallFlag);
}
As per me, every variable declared in javascript above functions treat as global. But in my case, i unable to access that variable show undefined
. Here is jsfiddle
code:
__getUserByRmId('sun1')
var firstCallFlag = 1;
function __getUserByRmId(rmId) {
console.log(firstCallFlag);
}
Share
Improve this question
asked Jan 17, 2017 at 6:28
user7104874user7104874
1,3813 gold badges16 silver badges21 bronze badges
5 Answers
Reset to default 3This is because the variable is defined after the function is called.
You can do this. It will work.
var firstCallFlag = 1;
__getUserByRmId('sun1')
function __getUserByRmId(rmId) {
console.log(firstCallFlag);
}
By the way, hoisting is JavaScript's default behavior of moving all declarations to the top of the current scope. So, something like this should work.
firstCallFlag =1 ;
__getUserByRmId('sun1')
function __getUserByRmId(rmId) {
console.log(firstCallFlag);
}
var firstCallFlag;
However, JavaScript Initializations are Not Hoisted. Thus, the code like below will consider the variable firstCallFlag undefined.
__getUserByRmId('sun1')
function __getUserByRmId(rmId) {
console.log(firstCallFlag);
}
var firstCallFlag = 1;
There is a term function and variable hoisting. When you create a function
like that and a variable with var
, they moved to the top of of all. But the functions
are hoisted first before variables
. So when you call the function
, it sees the function
, but doesn't see the variable
.
Engine parses your code to this and executes line by line. So it doesn't see the variable
.
function __getUserByRmId(rmId) {
console.log(firstCallFlag);
}
__getUserByRmId('sun1')
var firstCallFlag = 1;
You need to call your function after the variable declaration
var firstCallFlag = 1;
__getUserByRmId('sun1');
function __getUserByRmId(rmId) {
console.log(firstCallFlag);
}
Call the function after declaring the variable. Function will hoist first and then the variables.
var firstCallFlag = 1;
function __getUserByRmId(rmId) {
console.log(firstCallFlag);
}
__getUserByRmId('sun1');
If you still want to work with the same pattern of approach without declaring the variable on top of function call, then you should have a timeout call like this:
_getUserByRmId('sun1');
var firstCallFlag = 1;
function __getUserByRmId(rmId) {
setTimeout(function(){
console.log(firstCallFlag);
},1);
}
var firstCallFlag = 1;
function __getUserByRmId(rmId) {
console.log(firstCallFlag);
}
__getUserByRmId('sun1');
Its beacuse you have to define the variable "firstCallFlag " first and then call the function __getUserByRmId('sun1');
....variable hoisting in javascript
本文标签: Unable to access global variable in javascriptStack Overflow
版权声明:本文标题:Unable to access global variable in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742268005a2443773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论