簡體   English   中英

在javascript中訪問內部函數

[英]Accessing inner functions in javascript

如何在對login()調用中訪問buildLoginUrl() login() here的第一個沒有被打印,我認為是因為從不返回對login()的調用。

main.js:

$(document).ready(function() {
    // login function to kick off the authorization
    function login(callback) {
        console.log("here2");
        // builds the login URL for user after clicking login button
        function buildLoginUrl(scopes) {
            console.log("here3");
            return 'https://accounts.spotify.com/authorize?client_id=' + clientID +
              '&redirect_uri=' + redirectURI +
              '&scope=' + scopes +
              '&response_type=token';
        }

        // other stuff
    }
// event listeners
    $("#login").click(function() {
        // call the login function, we'll get back the accessToken from it
        console.log("here1");
        login(function(accessToken) {
            // callback function from login, gives us the accessToken

            //buildLoginUrl(scopes);
            var request = getUserData(accessToken);
            console.log("here");
            request.get(options, function(error, response, body) {
                console.log(body);
            });
            // other stuff
        });
     });

安慰:

here1
here2
here4

您將回調傳遞給登錄,但是login功能從不調用回調。 這就是為什么您永遠看不到“這里”的原因

here的第一個沒有被打印,我認為是因為從不返回對login()的調用。

為簡單起見:

// here yo are defining login()
function login(callback) {
    callback();
}

// here you are invoking login()
login(function() {
    console.log('here');
});

here不會被打印,因為您從未在login()內調用過回調; 您必須按上述方式調用callback才能在here打印。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM