Work these years, JS study is not very good, just weekend some leisure time, simply buy this "JS authoritative guide", the famous rhino book, Good to JS in-depth look. Bought this book.
The first impression of the book is that the thief is thick, but half the back part is a reference manual.
One: Scope
The first thing to say about variables is the scope, precisely because unfamiliar with the scope of JS, often will be the object-oriented scope pigtailed, after all, some things are always habitual
This, but not every time the copy is possible, then the next question is, what is the scope of JS, of course, the function scope, our browser is an instance
Window object, if you define a Name field under Window, the Name field has the function scope of window, which is accessible under window.
If a function ctrip is defined under window and then a name is defined inside, then the newly defined name can be used only under the Ctrip function, and the old name continues to
Window under General, for example.
You can see two points:
1: Define a name under window, incredibly can also define a name under function, which is unthinkable in C #.
2: In JS can be done blind, it only recognize their own scope, so there is the first "second", you may think this is no strange place, it is because you may still
Without really understanding what a function scope is, the first thing the parser does when executing a ctrip is to look for all the local variables under Ctrip and then execute the subsequent statements, since it is first
Look for, then Var name= "Second" this statement definition anywhere in the Ctrip is OK, below we exchange the statement.
Can see under the Ctrip function, the first console.log output is undefined, this result can be confirmed, did do the first thing is to collect the name of the local variable, may
Some people say that why does not become "second", that is because the initialization operation must be execution by statement, so in the Ctrip function to execute console.log (name), at this time the parser only know that there is a
An unassigned variable name, so the console is undefined.
Two: Scope chain
As we know from the above example, the variables defined in function only have scopes within the function scope, and we also see that the above example is just a layer
Nested, window is a large function, inside is a Ctrip function, the same principle can extend to multiple layers of nesting, such as three layers, four layers .... n layers, these layers form a
A chain-type structure.
As you can see, I define a plane function under Ctrip, so there are three layers, and the output is what we want to see, each layer's name is scoped to its own scope.
In effect, but there is a problem, one day I was stupid, in the definition of plane function, the Var name= "third" in the Var forget to write, then this time, plane
What is the value of name exactly? Is it first or second?
1 varName= "First";2 functionCtrip () {3 varName= "Second";4 functionplane () {5 name= "Third";6 Console.log (name);7 }8 plane ();9 Console.log (name);Ten } One Ctrip (); AConsole.log (name);
Now it's time to test if you really understand the scope chain, think about it, when the code executes to the Name= "third" in the plane function, it is found that the plane function does not have a local name.
Variable, exactly the code is in Ctrip this large function, so the parser will go back to the Ctrip function to look for name, found that there is a name, this time put Ctrip name
Changed to "third".
Another day, I drank a lot of wine and silly forced a back, in the definition of plane function, the name= "third" wrong written nam= "Third"; Lost an E, you can say it's the alcohol problem,
It's not the problem with my code. So what is the parser going to do with this time? The same truth, when backtracking, found that Ctrip did not, and then back to the top of the window, found that there is still no,
This time the parser did such a deal, since the whole chain is not, you have to assign value, I can not give you an error, that much awkward ah, simply give you in window under the implicit definition of a
Nam variable, this time Nam is actually a global variable. We can have a look at the top of window console for Nam.
Well, there are so many things about variables, no surprise, no sense of understanding.
JavaScript Tour-First: Starting with variables