JavaScript Scope Learning Notes

Source: Internet
Author: User

The amount ... Blogging or something like that's hard to say ... The brain is empty all of a sudden ~ ~

Forget it is not sentimental, as a smooth stroke ~ ~ is written to write on the high.

The JS scope can be easily understood in two words. From top to bottom, from inside to outside

(i) Pre-analysis

Let's take a look at the code

alert (a); var a = 2;

The result of the operation is undefined. When you declare a variable with VAR but do not initialize it, the value of the variable is undefined. That is a = undefined

function A () {console.log (1);} alert (a);

Operation Result:

In the pre-parsing phase, the JS parser will look for keywords var,function, variables, etc.

In this case a block of code is deposited, i.e. A = function A () {
Console.log (1);
}

Follow a simple rule in the global scope from top to bottom , look at a piece of code and guess the result.

alert (a); function A () {alert (4);} var a = 1;alert (a); function A () {alert (3);} alert (a);

Conclusion: JS pre-parsing encountered only one of the same name, variables and functions are the same name, leaving only the function

(ii) Implementation of the environment and the concept of scope

The execution environment defines the other data that a variable or function has access to, and determines their respective behavior. Each execution environment has a variable object associated with it. All variables and functions defined in the environment are stored in the variable object. After all code in an execution environment is executed, the environment is destroyed, and all the variables and function definitions stored therein are destroyed.

Each function has its own execution environment.

The execution environment of the function is destroyed after the execution of the FN1, and the variable a declared within the function is destroyed.

Forgive me for staying in primary school drawing level ~ ~

When the pilot stream enters a function, the function's environment is pushed into an environment stack. After the function executes, the stack pops up its environment and returns control to the previous execution environment.

Here to insert a sentence, like in my example, the function is synchronous drip ~ ~ Synchronous task in the execution stack ~ ~ Recommended reading Ruan a peak of the great God's JavaScript operating mechanism in detail: again on the event Loop

Www.ruanyifeng.com/blog/2014/10/event-loop.html

(iii) Scope chain

When code executes in an environment, a scope chain is created. A scope chain guarantees an orderly access to all variables and functions that the execution environment has access to. The front end of the scope is always the variable object of the environment in which the code is currently executing. The next variable object in the scope chain is from the containing (external) environment. The last object of the scope chain is always a variable object of the global execution environment. That is, the scope chain follows a simple rule from inside Out

Now make a slight change to my pupil level chart.

The scope chain of the FN1, starting with the variable object of FN1, and then from the variable object containing the environment, here is the global variable object.

Fn2 execution, execute a (), encounter arg1 will follow the scope chain to find Arg1, will first from the function a variable object to look for arg1, not to go to the scope chain on the next variable object fn2 the variable object, not on the scope chain on the next variable object in the global variable object to look for, Arg1 = True is found here.

To summarize, the internal environment can access all external environments through the scope chain, but the external environment cannot access any variables or functions in the internal environment.

(iv) No block-level scope

There is no block-level scope in JavaScript.

hehe = =, on this issue just remember this sentence is enough ~ ~ ... Of course not to say such a word, do not have some dry goods I am embarrassed = =

In other Class C languages, blocks of code enclosed by curly braces {} have their own scopes, but JS does not have a block-level scope !!!

If there is a block-level scope, it should be an error because a is a local variable and cannot be found globally.

However, run result a = 9, so there is no block-level scope.

(v) This

(vi) Call

JavaScript Scope Learning Notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.