This article describes JavaScript pre-parsing and related techniques. Share it for your reference, as follows:
variable
Again, start with the error comparison prompts of these two small examples.
alert(y1); //Code segment 1var y1 = 'dddd';alert(y2); //Code segment 2 // alert(typeof y2); y2 = 'xxxxx';
Let's first think about why one prompts undefined , and the other throws the error of undefined variables. . Let’s first look at the JavaScript parsing process.
Before the execution process, javascript will do an event "preparsement". The parsing engine performs the creation of all var variables at the block level and gives them an initial value: undefined. In this way, the reason why the first example pops up undefined is obvious.
So the first code is actually equivalent to
var y1;alert(typeof y1); //Naturally, its value is undefinedy1 = 'dddd';
Why did the second piece of code throw the wrong again? It no longer belongs to the "pre-parse" stage (here I assume that the browser only does two things when encountering a script tag: pre-parse and execution, which is actually not just these two things). However, in the execution stage, the reason for throwing the error is that js does not know the state of y2 in the execution segment state (no information of y2 was captured in the pre-parsement stage), and of course, it throws undefined error information. Another problem is involved here: js is a weak-type language, and variables can be used without definition, so why is it thrown here as a definition error?
There is always a reason for things to happen. JavaScript always has many strange features, and there is a variable that is read and write uneven. Undefined variables are only writable and not readable. What can be written? Everyone is familiar with this writing method:
y2 = 'exam'; //Before its definition operation appears (i.e. before it does not have its own scope), this operation will consider this code to define a global variable, register a property y2 on the window, and assign it to exam
But when reading it, the js engine cannot find any related information about it, so it acts with its own temper and makes an undefined mistake without hesitation. This is the rule of the game of js. And yet, why can you get its type? Remember js' operations on objects. If you access an object that does not exist, it will prompt undefined (because it is currently an attribute of the window object).
Note: It is necessary to distinguish here that the read and write uneven variables are only used for variables, and all the properties of objects are read. This feature does not exist. If it does not exist, it will prompt undefined.
in conclusion
At this point, my thoughts result: There are certain similarities in the writing operations of variables and objects. However, each has its own set of rules for reading operations. Because of this, the above problems are encountered.
In this way, the following question should be easily answered.
if (!('a' in window)) { var a = 1;}alert(a);function
To extend it, function. Remember the pre-parsement mentioned above. In the pre-parsement of javascript, in addition to the pre-definition of var variables, it also includes extracting the definition of the function, so the function can be defined anywhere in script and called anywhere. Not limited to before it.
However, the definition method of function includes a literal definition method, using the method of var to declare function. See below
alert(typeof y3); //Result?var y3 = function (){ console.log('1'); }Remember this agreement: the call must appear after the declaration. Why? If you understand the above, the answer here is actually clear. When pre-parsing var, the javascript engine will give them an initial value undefined. In this way, if we call it before its declaration, the javascript engine has not yet obtained its true value, it will naturally report the error of "xxx is not a function". This also clarifies why both function declarations are related to the order of declarations and calls, and the other has no such constraints.
in conclusion
It is a function, the result of js execution and dynamic modification, and it still follows the pre-analysis rules of variables (when alerting above, it did not get the information of the literal function).
What if it is a mixture of two. Look at the following, there are both variables and functions for y4.
alert(typeof y4); //Result?function y4(){ console.log('y4')}var y4;Because JavaScript has high priority in pre-parsing, y4 is naturally a function type, but after y4 is assigned (the js engine is in the process of execution), its assignment operation to js will override the function declaration. so:
alert(typeof y5);var y5 = 'angle';function y5(){ console.log('ghost'); }alert(y5);The first alert result is function because it is at the top of the js execution process. The second time when alert is renamed, its value has been rewritten to 5 (Don't be confused by the definition position of the function below.)
Thinking about the analysis and execution of js, I realized that I suddenly realized that the answers to many questions naturally surfaced. As the author of that article said, "Once you understand the concepts of execution environment, call objects, closures, lexical scope, and scope chains, many phenomena in JS language can be solved easily."
Looking back now, even in this incredible language, there are many reasons that can be traced back to it.
How to make better parameter judgments
After discussing so much above, how can it be closer to actual development? Since JavaScript's read and write unevenness, how can we avoid making parameter judgments without reporting errors?
eg:
if(cusVar){ //The judgment here is an implicit problem. }How to be more rigorous.
if(window['cusVar']) { // Ensure that it does not report an error. // Or such judgment is also feasible window.cusVar | typeof cusVar !== 'undefined' //Work}Finally, another small quiz is added, (understand the separation of pre-parsing and execution)
var y7 = 'test';function fun1(){ alert(y7); var y7 = 'sex';}fun1();For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.