更新时间:2022-11-11 15:11:45
问题不是闭包,问题是异步函数. $ .get()连接到服务器,并在服务器返回答案时运行其回调函数.但是$ .get()是在发送请求后完成的,而不是在返回响应时完成的.因此,在执行回调函数之前,最后两行console.log()行正在运行.
The problem isn't the closure, the problem is asynchronous functions. $.get() connects to a server, and runs its callback function when the server returns an answer. But $.get() completes once the request is sent, not when the response is returned. So your last two console.log() lines are running before the callback function is executed.
一旦执行了回调函数,您就只能访问headers
和countryData
变量,并且您知道发生的唯一地方是在回调函数本身内部.或它调用的其他代码.
You can only access the headers
and countryData
variables once the callback function has executed, and the only place you know that has happened is inside the callback function itself. or other code that it calls.