admin管理员组文章数量:1023744
My javascript function doesn't return anything. I really don't get it. Is it a problem of variable scope?
function getLanguage(){
navigator.globalization.getLocaleName(
function(locale){
var lan = locale.value;
},
function(){
var lan = null;
}
);
return lan;
}
Thank you!
My javascript function doesn't return anything. I really don't get it. Is it a problem of variable scope?
function getLanguage(){
navigator.globalization.getLocaleName(
function(locale){
var lan = locale.value;
},
function(){
var lan = null;
}
);
return lan;
}
Thank you!
Share Improve this question asked Dec 21, 2014 at 20:28 DescampsAuDescampsAu 1,0662 gold badges10 silver badges29 bronze badges 3-
3
Yes, scope is the issue. Define
lan
beforenavigator.globalization.getLocaleName
– Neil Turner Commented Dec 21, 2014 at 20:31 -
It is returning
lan
; did you check to see what its value is? – Scott Hunter Commented Dec 21, 2014 at 20:33 -
If you want to avoid callback-soup use
Promises
: developer.mozilla/en/docs/Web/JavaScript/Reference/… – Halcyon Commented Dec 21, 2014 at 20:43
1 Answer
Reset to default 4This is a duplicate of the old asynchronicity problem, but there's a second issue as well -- scope.
First of all, scope. The lan
variable is defined inside the internal function, and so cannot be seen from outside.
function getLanguage(){
var lan;
navigator.globalization.getLocaleName(
function(locale){
lan = locale.value;
},
function(){
lan = null;
}
);
return lan;
}
That was easy. But it still won't work, due to asynchronity. You have to set up your function to use a callback instead:
function getLanguage(callback){
navigator.globalization.getLocaleName(
function(locale){
callback(locale.value);
},
function(){
callback(null);
}
);
}
Also, by now, we don't even need the variable, so i got rid of it.
Then, you call it as:
getLanguage(function(lan){
// something with lan here
});
My javascript function doesn't return anything. I really don't get it. Is it a problem of variable scope?
function getLanguage(){
navigator.globalization.getLocaleName(
function(locale){
var lan = locale.value;
},
function(){
var lan = null;
}
);
return lan;
}
Thank you!
My javascript function doesn't return anything. I really don't get it. Is it a problem of variable scope?
function getLanguage(){
navigator.globalization.getLocaleName(
function(locale){
var lan = locale.value;
},
function(){
var lan = null;
}
);
return lan;
}
Thank you!
Share Improve this question asked Dec 21, 2014 at 20:28 DescampsAuDescampsAu 1,0662 gold badges10 silver badges29 bronze badges 3-
3
Yes, scope is the issue. Define
lan
beforenavigator.globalization.getLocaleName
– Neil Turner Commented Dec 21, 2014 at 20:31 -
It is returning
lan
; did you check to see what its value is? – Scott Hunter Commented Dec 21, 2014 at 20:33 -
If you want to avoid callback-soup use
Promises
: developer.mozilla/en/docs/Web/JavaScript/Reference/… – Halcyon Commented Dec 21, 2014 at 20:43
1 Answer
Reset to default 4This is a duplicate of the old asynchronicity problem, but there's a second issue as well -- scope.
First of all, scope. The lan
variable is defined inside the internal function, and so cannot be seen from outside.
function getLanguage(){
var lan;
navigator.globalization.getLocaleName(
function(locale){
lan = locale.value;
},
function(){
lan = null;
}
);
return lan;
}
That was easy. But it still won't work, due to asynchronity. You have to set up your function to use a callback instead:
function getLanguage(callback){
navigator.globalization.getLocaleName(
function(locale){
callback(locale.value);
},
function(){
callback(null);
}
);
}
Also, by now, we don't even need the variable, so i got rid of it.
Then, you call it as:
getLanguage(function(lan){
// something with lan here
});
本文标签: Javascript function doesn39t return anythingStack Overflow
版权声明:本文标题:Javascript function doesn't return anything - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745551025a2155633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论