stumped
stumped

Reputation: 3293

Accessing inner functions in javascript

How would I access buildLoginUrl() in my call to login()? The first here isn't being printed, and I think it's because the call to login() is never returned.

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
        });
     });

Console:

here1
here2
here4

Upvotes: 0

Views: 60

Answers (2)

Robert
Robert

Reputation: 10390

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

To keep it simple:

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

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

here doesn't get printed because you never invoked the callback within login(); you have to call callback as shown above in order for here to be printed.

Upvotes: 0

jas7457
jas7457

Reputation: 1782

You are passing a callback to login, but the login function never calls the callback. That's why you'd never see "here"

Upvotes: 1

Related Questions