访问 javascript 中的内部函数

Accessing inner functions in javascript

如何在调用 login() 时访问 buildLoginUrl()?第一个 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"

The first here isn't being printed, and I think it's because the call to login() is never returned.

为简单起见:

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

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

here 没有被打印,因为你从未在 login() 中调用过回调;您必须如上所示调用 callback 才能打印 here