Grilled one grilled JavaScript pre-explained _javascript tips

Source: Internet
Author: User
Tags function definition

With var keyword pre-explained

Let's take a look at the results of this code execution:

Copy Code code as follows:

alert (n);//Eject undefined
var n = 10;

The result of the pop-up is undefined, why not 10? Let's look at the results of the following code execution:

Copy Code code as follows:

alert (n);
n = 10;

Run the following error:

Why the error this time, because the code in the runtime, did not declare the variable N; Through the comparison of these two pieces of code, we find that there is a difference between the var keyword and the variable declared without the var keyword, the variable with the Var declaration before the code executes, It seems that browsers have given them an initial value of undefined, so before we execute the code, the browser engine automatically scans the process with the VAR keyword and the variables and defined functions declared with the function keyword (mentioned later) as the pre-interpretation.

With function keyword Pre-interpretation

Let's look at the results of the following code execution:

Copy Code code as follows:

FN ();//Popup Hello
function fn () {
Alert (' Hello ');
}

The execution result pop-up Hello,fn is able to execute correctly because FN was previously explained before the code was executed, and the FN definition (defined) was already defined at the time of the interpretation, and we have questions about why the first code execution result does not eject 10, but undefined, The Declaration and definition in another concept JavaScript are introduced again.

Statements in JavaScript (declare) and definitions (defined)

We usually use the VAR key to declare variables and function keywords to define functions, except that the function keyword declares and defines functions that are executed concurrently, while Var only declares variables and does not have a defined function.

The following are variables declared with the VAR keyword:

Copy Code code as follows:

var n;//declares a variable n
var m = 10;//declares a variable m and assigns 10 to it

The following functions are defined with the Function keyword:

Copy Code code as follows:

Defines a function of FN
function fn () {
Alert (' Hello ');
}

difference between the var keyword and the function keyword pre-interpretation

In fact, the difference between the two is that with the VAR keyword the pre-interpretation only interprets the declaration part (because it does not have the ability to define itself), and the function keyword is declared and defined at the same time as the predefined interpretation. Then we go back to analyze the first paragraph of code, the analysis is as follows:

A preliminary explanation of the absence of integrity (pit dad)

Why do you say it has no integrity, please see the following code (excluding Firefox):

Copy Code code as follows:

alert (n);
FN ();
if (false) {
var n = 10;
function fn () {
Alert (' Hello ');
}
}

The first line of code execution pops up the undefined, and the second line of code pops up Hello, because N and FN are interpreted before the code executes, even if the if condition is judged to be false, The persistent browser engine also scans the variable n with the var keyword and the FN with the function key definition.

* Pre-explanation ignores the re-declaration without ignoring the redefinition

This place is a little bit more round and not very well understood, so add an asterisk, see the following code:

Copy Code code as follows:

alert (n);
var n = 10;
var n = 9;
var n;
alert (n);

What the result of this code execution is, let's analyze:

To continue with the code, analyze the following execution results:

Copy Code code as follows:

FN ();
function fn () {
Alert (' 1 ');
}
FN ();
function fn () {
Alert (' 2 ');
}
FN ();

The code Analysis diagram is as follows:

Functional pre-interpretation analysis with function definition

Summarize:

This blog with a large number of code and screenshots of JavaScript in the pre-interpretation of the overview, throughout a variety of books on the interpretation of the description is very little, in fact, the usual use of the scene in the work is not much, unfortunately, the preliminary explanation is the major companies face test questions. When I first started touching it, it felt like it was always writing code without the usual logic, but sometimes it worked and it didn't, and of course it added to our sense of mystery, and let's learn more about how the browser engine interprets the execution of our code. Hereby in the post I will take a few classic cases to synthesize the analysis of it, and again thank you for your support!

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.