JavaScript pre-parsing and related skills analysis _javascript skills

Source: Internet
Author: User
Tags script tag

The examples in this article describe JavaScript pre-parsing and related techniques. Share to everyone for your reference, specific as follows:

Variable

Again, start with the error-contrast hints for these two small examples.

alert (y1);     Code Snippet 1
var y1 = ' dddd ';
alert (y2);     Code segment 2//alert (typeof y2);
y2 = ' xxxxx ';

Think first, why one prompts undefined, one throws the variable undefined error. Read the JavaScript parsing process first.

JavaScript does an event "pre-resolution" before executing the procedure. The parsing engine performs the creation of all Var variables at the block level and gives them an initial value: undefined. Thus, the reason why the first example pops up undefined is obvious.

So the first piece of code is actually equivalent to

var Y1;
Alert (typeof y1); Naturally, it is now worth undefined
y1 = ' dddd ';

Then why is the second piece of code wrong again, this time no longer belong to the "pre-resolution" phase, (here I assume that the browser encountered a script tag only two things: pre-resolution and execution, in fact, not only these two things), but in the implementation phase, The reason for the mistake is that JS does not know the state of the Y2 in the execution segment State (no information in the pre-resolution phase captures the Y2), and of course throws an undefined error message. Here is another problem: JS is a weak type language, the variable is not defined can be used, then why is thrown here to define errors.

There is always a reason for this, JavaScript has a lot of its own strange characteristics, there is a variable called the reading and writing is not equal. Undefined variables, which are only writable and not readable. What is writable, and everyone is familiar with the wording:

y2 = ' exam ';  
The operation will define a global variable, register a property y2 on the window, and assign the value to exam before its definition operation occurs (that is, before it has its own scope).

But in the read operation of it, the JS engine can not find any relevant information about it, with its own temper, not to mention an undefined mistake, this is the rules of the game JS. And yet, why is it possible to get the type of it? Also remember the operation of JS to the object bar. If you access an object that does not exist, you will be prompted for undefined (because it is currently a property of the Window object).

Note: Here you need to differentiate, the variable's read and write inequality is only used for variables, read the properties of all objects, do not exist the attribute, if it does not exist, will prompt undefined.

Conclusion

Here, my thinking results: they have a certain similarity to the writing of variables and objects. But the reading operation, each has a set of rules, because of this, so there are problems above.
So, the following question should be easy to get the answer.

if (! (' A ' in Window ') {
  var a = 1;
}
alert (a);

Function

By extension, function. Remember that pre-resolution mentioned above, in the pre-parsing of JavaScript, in addition to the predefined var variables, also includes the extraction of the definition of the function, so you can define the function anywhere in the script, called anywhere. Not limited to it before.

But the definition of a function, including a method called literal definition, uses VAR to declare a function. Look down.

Alert (typeof Y3); The result?
var y3 = function () {Console.log (' 1 ');}

Remember this convention: The call must appear after the declaration, why, if understood above, in fact, the answer here is clear. The JavaScript engine gives them an initial value undefined when parsing Var, so if we call it before its declaration, the JavaScript engine does not get its real value, and it naturally reports the "XXX is not a function" error. It's okay. Clear why the same as function declaration, one is related to the order of declarations and calls, one has no such constraints.

Conclusion

It is a function, JS execution, the result of dynamic modification, still follow the variables of the pre-resolution rules (when alert, it did not get the literal function of the information).
What if it's a two-a-mix? Look below, there are also variables and function for Y4.

Alert (typeof Y4); The result?
function Y4 () {
  console.log (' Y4 ')
}
var y4;

Because JavaScript has a higher precedence for the function when it is resolved, y4 is naturally a function type, but when the Y4 is assigned (at which point the JS engine is executing), its assignment to JS overrides the function declaration. So:

Alert (typeof Y5);
var y5 = ' angle ';
function Y5 () {
  console.log (' ghost ');  
}
alert (Y5);

The first alert result is a function because it is at the top of the JS execution process. At the second alert, its value has been rewritten to 5 (not to be fooled by the position of the function's definition). )

From the analysis and implementation of JS separate to think, found that the immediate enlightened, many of the answers to the questions are naturally surfaced, as the author of the article said, "Once the implementation of the environment, call objects, closures, lexical scopes, scope chain of these concepts, JS language Many phenomena can be solved." "

And now, even in this incredible language, there are a lot of reasons to go back to it.

How to make the parameter judgment better

discussed the above so much, that how to make it closer to the actual development, since JavaScript is not equal to read and write, how to avoid the error in the case of a parameter to judge it.

eg

if (Cusvar) {//Here is the judgment, is there an implied problem? }

How to be more rigorous.

if (window[' Cusvar ']) {//Make sure it doesn't complain. /
  or This kind of judgment is also feasible Window.cusvar | typeof cusvar!== ' undefined '
  //work


Finally add another small quiz, (understanding the separation of pre-resolution and execution)

var Y7 = ' Test ';
function fun1 () {
  alert (Y7);
  var Y7 = ' sex ';
}
Fun1 ();

More readers interested in JavaScript-related content can view the site topics: "JavaScript switching effects and tips summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and tips summary", " JavaScript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical calculation usage Summary

I hope this article will help you with JavaScript programming.

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.