Understanding javascript prototype and scope series (12) -- Introduction [Scope], javascript prototype

Source: Internet
Author: User

Understanding javascript prototype and scope series (12) -- Introduction [Scope], javascript prototype

For the articles in the previous sections, refer to understanding the javascript prototype and scope series.

When it comes to scope, you may be familiar with one sentence (with js development experience): "javascript has no block-level scope ". The so-called "Block" refers to the statement in the middle of the braces. For example, if statement:

For example, the for statement:

Therefore, when writing code, do not declare variables in blocks. We should declare the variables at the beginning of the Code. To avoid ambiguity. For example:

 

In fact, it is not enough to know that "javascript has no block-level scope". What you need to know is --In addition to the global scope, javascript only has the scope that can be created by functions..

Therefore, when declaring variables, the global code should be declared at the front-end of the Code, and the function should be declared at the beginning of the function body. No variable Declaration exists in other places. We recommend that you use the single var format.

JQuery is a good example:

 

 

Next we will continue with the scope. Scope is a very abstract concept, similar to a "territory"

For example, the global code, fn, and bar functions form a scope. In addition, the scope has a parent-child relationship. The determination of the parent-child relationship depends on the scope in which the function is created. For example, if the bar function is created under the fn scope, the "fn scope" is the upper level of the "bar scope.

 

The biggest use of a scope is to isolate variables. variables with the same name in different scopes do not conflict. For example, in the above Code, the variable "a" is declared under the three scopes, but they do not conflict. Use "a" under their respective scopes ".

Now, let's talk about jquery source code.

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

Why?

The reason is that a large number of variables are declared in jQuery Source Code. These variables will be restricted to an independent scope through a function, it does not conflict with variables of the same name in the global scope or other function scopes.

Developers all over the world are using jQuery. If you do not do this, it is likely that the variables in jQuery source code are duplicated with those in external javascript code, resulting in conflicts.

 

The scope is not easy to explain. Let's take a small step and show it to you step by step.

The next section describes how to combine the scope and execution context.

Obviously, to understand the closure, it is not clear to say one or two sentences...

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

This article has been updated to the "Understanding javascript prototype and scope series" directory. For more information, see "Understanding javascript prototype and scope series".

In addition, you are welcome to follow my microblog.

You are also welcome to follow my other tutorials-don't worry, they are all free!

Microsoft petshop4.0 source code explanation video json2.js source code interpretation video

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


Is the javascript scope chain related to the prototype chain? From the principle analysis, do not say what I found online

In my personal opinion, there is no contact.
In general, the scope chain is for variables. For a large range in js, there are only two scopes: global scope and internal function scope, if function 2 is defined in function 1 (usually anonymous), a global scope of function 1 is available; the feature is that the global scope variables can be used directly in function 1, and the global scope and function 1 Scope variables can be used directly in function 2.
The prototype chain is generally used to define constructors. It can be considered to be for constructors or classes corresponding to constructors. the header of the prototype chain is the Object class/constructor, if there is a constructor 1. prototype = constructor 2; then there is such a prototype chain; Object => constructor 1 => constructor 2, the advantage is that constructor 2 corresponds to a class, you can have attributes in constructor 1 and Object. js does not have the corresponding inherited keywords. Therefore, you can use the prototype chain to simulate the inheritance effect.
I hope it will help you

Javascript scope Problems

This. B = 100;
This. c = {
D: function (){
Alert (this. B); // undefined will pop up here. How can I display 100 in function?
}
}
}
Var I = new ();
I. c. d. call (I );

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.