what’s the difference between them ?
①function’s scope–>②[[scope]]—->③scope
var scope = 'window';
var someFunction = function(){
something go here....
}
someFunction’s scope
someFunction[[scope]]
window scope
is that right?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you want to understand the scope chain, you should read Richard Cornford’s article on JavaScript Closures, particularly the part on Identifier Resolution, Execution Contexts and scope chains.
Briefly, the scope chain travels from variable/activation object to variable/activation object, stopping with the global object (since the global object is effectively the activation/variable object for the global execution context).
So in the case of:
The identifier fred is made a property of the global variable/activation object (which in the case of global code is the global objet itself). When foo() is called, a new variable/activation object is created with a scope chain that includes the global object. The identifier fred is first resolved on the internal variable object, and since it won’t be found there, the next object on the chain (the global object) is searched.
Similarly for:
So when foo() is called, it then calls bar(), and now fred is resolved against firstly bar’s variable object, then foo’s, then the global object.
Once you understand execution context, you will also realise why calling the value of this “context” is misleading.