为什么我的变量在回调函数之外没有定义
Why are my variables undefined outside of the callback function?
我在函数中设置了一些变量。当在函数内部时,我可以获取、设置并提醒uid1
和accessToken2
,但如果我试图在函数外部提醒它们,它会给出undefined
。如何设置值?
这是代码:
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid1 = response.authResponse.userID;
alert(uid1); //works here
var accessToken2 = response.authResponse.accessToken;
alert(accessToken2); //works here
}
else if (response.status === 'not_authorized') { }
else { }
});
alert(uid1); //does NOT work here
alert(accessToken2); //does NOT work here
您在使用这些变量的范围之外声明了这些变量。要修复代码,请在函数外声明它们:
var uid1 = "";
var accessToken2 = "";
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
uid1 = response.authResponse.userID;
alert(uid1);
accessToken2 = response.authResponse.accessToken;
alert(accessToken2);
} else if (response.status === 'not_authorized') {
} else {
}
alert(uid1); // Values will be set here.
alert(accessToken2);
});
alert(uid1); // Values won't reliably be set here.
alert(accessToken2);
更新:正如下面的注释所建议的,因为getLoginStatus
方法是异步的,所以在方法外部调用alert()
时可能没有值。我在回调中添加了额外的警报,以显示您应该在哪里尝试访问这些值。
您似乎在考虑您的代码,就好像回调函数是在之前执行的一样
alert(uid1); //does NOT work here
alert(accessToken2);
由于FB.getLoginStatus可能是异步的,所以情况并非如此。它将立即返回并继续显示您的警报。这里的问题不仅仅是可变范围。问题是,在执行回调之前,您无法访问要显示的信息。您无法通过移动变量声明来进行编程。你必须在你的程序/网站/任何东西的设计中考虑到这一事实。
Javascript中的变量具有函数作用域。这意味着它们只存在于用var关键字声明的函数中,除非它们是全局变量。将var关键字从函数中移出,但为了避免它们全局地将其封装在另一个函数中,如下所示:
(function(){
var uid1, accessToken2;
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
uid1 = response.authResponse.userID;
alert(uid1); works here
accessToken2 = response.authResponse.accessToken;
alert(accessToken2); //works here
} else if (response.status === 'not_authorized') {
} else {
}
});
alert(uid1); //uid1 declared but not set until callback executes
alert(accessToken2); //accessToken2 declared but not set until callback executes
// these alerts will likely NOT display the information
// they are intended to display because of asynchronous callback
})();
alert(uid1); //uid1 is not declared, so this doesn't work
alert(accessToken2); //accessToken2 is not declared, so this doesn't work
因为JavaScript(以及所有编程语言曾经)有作用域?
相关文章:
- 用嵌套函数和默认函数定义函数
- 自定义函数中的光标位置
- Jquery未定义函数正在停止其他操作
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- javascript无法重新定义函数内部的全局对象
- 定义函数时,如何捕获外部变量的当前值
- 能够在定义函数表达式之前使用它
- Google Sheet自定义函数返回0
- Javascript-defineProperty和直接在对象上定义函数之间的区别
- 其中是自定义函数中的属性
- 创建自定义函数以在函数上运行完整的多选下拉列表
- Google Sheets自定义函数条件格式
- 为什么当我需要位于顶部的函数时,在脚本中的某些点上没有定义函数
- 使用变量的名称,然后为该变量定义函数
- dalek回调或自定义函数
- 如何在angular ui模态控制器中定义函数
- Uncaught ReferenceError:尝试在Android网络视图中访问时未定义函数
- UI网格:如何从自定义函数访问MODEL_COL_FIELD
- 在构造函数中定义函数会比将其附加到原型消耗更多的内存
- 使用 Javascript 中的函数重新定义函数