so far, you know the history of JavaScript and the variable system of what you want to be. With your deep Java or C + + skills, and with the confidence of the programmer, you will surely be confident that writing JavaScript is no pressure. I also believe that writing a script for the back-end siege will certainly be a cinch. However, when the results of the bandits thought, you may be a spit slot: True TM Ghost, what is the bug? Or is there a problem with the browser? My code logic is fine .... Just like the code below, can you tell what the result is?
varA=123;varb=999;functionfunc(a) {varB; Console.Log(a);//?????? What is the result???? Keep the analysis.varA=888; C=1111;functiona(){ }Console.Log(a);//?????? What is the result???? Keep the analysis.Console.Log(b);//?????? What is the result???? Keep the analysis.Console.Log(C);//?????? What is the result???? Keep the analysis.}func(456);
Yes, there is no problem with your code, and of course the browser is OK. You might say I'm not going to write full screen "a" Code! Really, when you see this code, do you ever wonder why JavaScript can repeat a variable with the same name? The building will bet 10 spectators, one can ask this question, it is already a surprise. Some may say, "because it is a weak type of speech," the answer is only half right. This seemingly unscientific, very non-rigorous variable definition, how can it be run up? It's obviously not scientific. The answer is: Someone moved your code!
Someone has moved your code! Someone has moved your code! Someone has moved your code! It's important to say three times! Then who moved your code? The story begins again.
It has to go back to the 90 's when JavaScript was born. In the words Brandon-Ike was creating JavaScript at the time, his need was to do client-side data validation. So, he thought, "there is no need to engage in high-energy design, it seems like there is no need for high-energy computing, to do precompiled, linker that is too wasteful, and this is playing in the browser running, compiler, linker, that browser is not the IDE?" It's best to be able to run the most beautiful side of the analytic side like Perl. " Shoes with the see here should understand: So much nonsense, you are not to say that JavaScript is the edge of the analytic side of the run! I know, it says in this textbook. But a lot of textbooks seem to only say the side of the analytic side of the run, but did not say how to parse, even if there is said, it is more nonsense than this blog, also said not clear. Here, the front shouting three answers to the question, presumably crossing to this also see the answer: The parser moved your code!
The parser moved your code! That must be serious. "From the moment you hit the code, and then run, the final output" what happened to this process? Textbooks have said "edge resolution, side run", there is no doubt that the process is divided into "analytic period" and "run-time." Let's take the above code as an example to see how your code is going to run after it has been passively manipulated.
Resolution Period
First of all, the interpretation of the interpretation of the landlord: the Analytic period is each A runtime unit before the code is run, the parser adjusts the user code (code written by the programmer) for a period of time. Here is a key term for the "Run unit". What is a running unit? This is only illustrated in the browser environment (the NODEJS environment may not be the same). Simply understood, a page is a running unit, and a function is a running unit. A page of JavaScript before running, all of the page's javascript declaration definition All are parsed and adjusted once, and all in this function before a function is run. javascript declaration definition (including formal parameters) javascript Declaration definition ". So what is a declaration definition? And look at the code:
var a;//is a declaration definition var a=123;//contains a declaration definition, an assignment operation expression function f () {//is a function definition } var f=function () {// Contains a declaration definition, a function assignment operation expression }
Crossing if you have the patience to see this, you should understand what is the parsing period and what the JavaScript declaration definition is. This building again emphasizes that "the parser only defines the declaration" for parsing and adjusting, like the above "var a=123", "var f=function () {}" will be split into two parts, declaration definition and assignment operation! The declaration definition is used for the parsing period, and the assignment runs for the run time. How does the parser parse the definition of the statement that adjusts JavaScript? The following is the first paragraph of the blog to give a question of the code of the Func function to do distribution analysis.
First step: JavaScript runtime, Discovery ready to call Func (456)
Step Two: Func is a function execution unit that needs to parse and adjust before executing
Step three: Prepare a current Activityobject activity object for the Func execution unit, i.e. generate a so-called active object within the Func execution unit, the pseudo-code is: Var ao={};
Fourth step: First parse the func parameter definition, find that func defines a parameter a, then hang a to the AO object and assign the argument to the parameter, ao={a:456}
Fifth step: Parse the variable declaration definition, find the definition of Var B,ao={a:456,b:undefined}
Sixth step: Parse the variable declaration to define var a=888, split into Var a;a=888, find that the AO has a definition, do not adjust, ao={a:456,b:undefined}
Seventh Step: Parsing the function definition, discovering function A () {} functions definition, ao={a:function () {},b:undefined}
How about it! Crossing, know how the parser moved your code. All the declaration definitions you wrote are moved to an active object! Keep in mind that the parser moves your code this way: Prepare the object, parse the parameter and assign the argument, and then parse the var variable declaration definition within the function ( if it contains an assignment, split the assignment Operation ), and then parse the function definition.
So far, the parser rescue's work is done, everything is ready, only owes running! What's that running? The rest of the code is running, such as Var a=888, c=111, Console.log (). Is what is going to happen in the run time. Then, talk about the run-time thing, the results will be decided!
Operating period
Run time, that is the direct run code, there is no definition of what to say. But there's a surprising part of the run-time. This guy every encounter a variable (including a function variable), will first look for the existence from the current activityobject, if not exist then look up (chain of action?). Prototype chain? The next blog post is reserved). This strange behavior creates the magical variable-lift effect mentioned in the previous blog post. Crossing, you finally know what is the variable promotion, but also know what the devil caused by the variable promotion! Good! Let's cut the crap, and we'll do the rules. Analyze how the run time runs the code.
First step: Run Console.log (a), Find Ao object, find A=function, so the first result is function () {}
Step two: Run Var a=888, find Ao object, find a definition, perform assignment operation, at this time ao={a:888,b:undefined}, function is overwritten!
The third step: Run c=1111, find AO object, no goods! Look up, or there is no goods, all right, there is no goods, it can only be left to the father, so C became a member of the father, and assigned a value of 1111
Fourth step: Run Console.log (a), find the Ao object, find the material, a=888, the result is 888
Fifth step: Run Console.log (b), find AO object, found that there is material, b=undefined, results undefined, special statement: undefined and XXX are not defined are two different!
Sixth step: Run Console.log (c), find AO object, no goods, find Father adult, found father Adult has a c=1111, the result is 1111
You crossing, the time is not early, to see the writing is almost. After reading this blog, you should know that the code we write is passive and then run.
JavaScript who moved your code