我有一些變量,我正在設置一個函數。當我可以獲取,設置並提醒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
對於未來的讀者,我建議對這個問題進行編輯,以反映變量是在回調函數中設置的。正如所寫的,它看起來像變量範圍是唯一的問題。變量範圍是這個問題相關的兩個問題之一。 – 2012-02-27 05:25:05