Understanding JavaScript Prototypes and Scope series (12)--Introduction to "Scope"

Source: Internet
Author: User

Referring to the scope, there is a sentence that everyone (with JS development experience) may be more familiar with: "JavaScript does not have block-level scope." The so-called "block" is the statement in the middle of the curly brace "{}". For example, if statement:

Another example is the FOR statement:

So, when we write the code, do not declare the variable in the "block", to declare it at the very beginning of the code. To avoid ambiguity. Such as:

In fact, you know that "JavaScript does not have block-level scope" is not enough, you need to know-JavaScript In addition to the global scope, only the function can be created by the scope .

So, when we declare a variable, the global code is declared at the front of the code, and the function is declared at the beginning of the function body. In addition to these two places, there is no declaration of variables anywhere else. It is also recommended to use the form "single Var" .

jquery is a good example:

Continue to the scope below. Scope is a very abstract concept, similar to a "site"

For example, global code and FN, bar two functions will form a scope. Moreover, the scope of the relationship between the subordinate, the determination of the subordinate relationship depends on which scope the function is created . For example, when the bar function is created under the FN scope, the FN scope is the ancestor of the bar scope.

the most useful use of scopes is to isolate variables, and there will be no conflicts between variables of the same name in different scopes. For example, in the above code, the variable "a" is declared under three scopes, but they do not conflict. Individual scopes, with individual "a".

Speaking of which, we can also come up with the jquery source to talk about.

The outermost layer of the jquery source code is an automatically executed anonymous function:

Why did you do it?

The reason for this is that in the jquery source code, a large number of variables are declared, which will be limited to a separate scope through a function, without conflict with a variable of the same name as the global scope or other function scope.

Developers all over the world are using jquery, and if you don't, it's likely that the variables in the jquery source code will have the same name as the variables in the external JavaScript, creating a conflict.

Scope this piece is just very bad explanation, we will run slowly, step by step to show you.

The next section combines the scope and execution context to say.

Visible, to understand the closure, not one or two words can be said clearly ...

---------------------------------------------------------------------------

This article has been updated to the directory of Understanding JavaScript prototypes and scope series, and more on understanding JavaScript prototypes and Scope series.

Also, please pay attention to my Weibo.

Also welcome to follow my other tutorials-don't be afraid, are free!

"Microsoft petshop4.0 Source Interpretation Video" "Json2.js Source Interpretation video"

--------------------------------------------------------------------------

Understanding JavaScript Prototypes and Scope series (12)--Introduction to "Scope"

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.