据我所知,只有函数表达式的声明部分被提升而不是初始化。例如:
var myFunction = function myFunction() {console.log('Hello World');};
所以“var myFunction;” 被提升,但“function myFunction()...”没有。
现在我的问题是,我玩了一下 google auth 功能:
"use strict";
$(document).ready = (function() {
var clientId = 'MYCLIENTID';
var apiKey = 'MYAPIKEY';
var scopes = 'https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.readonly https://www.googleapis.com/auth/drive.appfolder https://www.googleapis.com/auth/drive.apps.readonly https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.install https://www.googleapis.com/auth/drive.metadata https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/drive.photos.readonly https://www.googleapis.com/auth/drive.scripts';
$('#init').click(function() {
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth(false, handleAuthResult), 1);
});
var checkAuth = function checkAuth(imm, callback) {
gapi.auth.authorize({
client_id: clientId,
scope: scopes,
immediate: imm
}, callback);
};
var handleAuthResult = function handleAuthResult(authResult) {
if (authResult) {
gapi.client.load('drive', 'v2', initialize);
} else {
$('#progress').html('Anmeldung fehlgeschlagen');
}
};
// Other code
})();
在第 10 行“window.setTimeout(checkAuth...”) 上,我调用了在此函数调用下方声明的 checkAuth 函数。我的假设是我收到错误消息,说“...checkAuth 不是函数/未定义等。.. .",但它确实有效。有人可以向我解释一下吗?