如何在我致电login()
时访问buildLoginUrl()
?第一个here
没有被打印,我认为这是因为对login()
的调用从不返回。在javascript中访问内部功能
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
你不在你的例子中的任何地方调用'buildLoginUrl'。你想在哪里打电话? –
除非将其分配给可访问(例如全局)变量或从函数返回,否则无法从封闭函数外部访问它。你为什么不直接在匿名函数中声明它? – Harald
'buildLoginUrl()'永远不会调用,所以'here3'永远不会显示。 –