Explanation of the scope and scope of Javascript variables, and explanation of javascript Variables

Source: Internet
Author: User

Explanation of the scope and scope of Javascript variables, and explanation of javascript Variables

Over the past few years, js has not learned very well. It is just some idle time on weekends. I just want to buy this "js authoritative guide" and the famous rhino book. I will take a closer look at js. The first impression I have bought this book is that I am a thief, but half of it is a reference manual.

I. Scope

When talking about variables, the first thing we need to talk about is scope. It is precisely because we are not familiar with JS scopes that we often stick to the object-oriented scope. After all, some things are always habitually like this, but not every copy is possible, so the next question comes: What is the scope of js? Of course, it is the function scope. Our browser is an instantiated window object, if a name field is defined in window, the name field has the function scope of window, that is, it can be accessed in window. If a function ctrip is defined in window, then define a name in it. The new name can only be used under the ctrip function, while the old name will continue to be used under the window. For example.

We can see two points:

1: A name is defined in the window, and a name can be defined in the function, which is unimaginable in C.

2: In JS, you can be blind. It only recognizes its own scope, so the first "second" appears. You may think there is nothing unusual about this, this is because you may not really understand what the function scope is. When the parser executes ctrip, the first thing is to find all the local variables under ctrip and then execute subsequent statements, since we are looking for it first, the var name = "second" statement can be defined anywhere in ctrip. Next we will replace the statement.

The first console under the ctrip function is displayed. the log output is undefined. This result confirms that the first thing we did was to collect the local variable name. Some people may say why it was not changed to "second". this is because the initialization operation must be executed by statement, so the console is executed in the ctrip function. log (name), at this time, the parser only knows that there is an unassigned variable name, so it is undefined in the console.

II. Scope chain

From the above example, we can clearly understand that the variables defined in the function only have the scope within the function range. At the same time, we can see that the above example is just a layer of nesting, window is a large function, which is a ctrip function. In the same way, it can be extended to multiple layers of nesting, such as Layer 3 and Layer 4 .... N layers form a chain structure.

As you can see, I have defined a plane function under ctrip, so there will be three layers. The output result is also expected. The name of each layer is only within the scope of its own scope.

But there is a problem below. One day I was dumb. when defining the plane function, I forgot to write the var in var name = "third". At this time, in plane

What is name? Is it first or second?
Copy codeThe Code is as follows:
Var name = "first ";
Function ctrip (){
Var name = "second ";
Function plane (){
Name = "third ";
Console. log (name );
}
Plane ();
Console. log (name );
}
Ctrip ();
Console. log (name );

Now it is a test to test whether you really understand the scope chain. If you think about it, you will find that when the code is executed to the name = "third" in the plane function, it is found that the plane function does not have the local variable "name", and the code is in the ctrip function again. Therefore, the parser will trace back to the ctrip function to find the name, and it is found that there is a name, in this case, change the ctrip name to "third".

One day, I drank too much wine and made a silly attempt. when defining the plane function, I mistakenly wrote name = "third" into nam = "third"; I lost an e, you can say it's alcohol,

It is not a problem with my code. What should the parser do at this time? In the same way, when we trace back, we find that the ctrip does not exist, and then we trace back to the top-Layer window,

At this time, the parser has done this. Since there is no such thing in the chain, and you have assigned another value, I cannot always report an error to you, simply define an implicit definition for you in the window.

Nam variable. At this time, nam is actually a global variable. We can look at nam in the top-level console of the window.

Well, there are so many things about variables. There's nothing unusual. It's boring to understand them.

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.