Do you really understand JavaScript?

Source: Internet
Author: User

Topic One:

If(! ( "A"in window){var=1;} Alert(a);              

Topic Two:

var=1,=function a(x){&& a(---x) ; }; Alert(a);               

Topic Three:

function a(x){return*2;} var a;  Alert(a);                

Topic Four:

function b(x, y, a) { arguments[2] =   Alert(a);} b(1, 2, 3);           

Topic Five:

function a(){ alert(this);} a.  Call(null);             

Please do not use any help tools, mental arithmetic answer. The answer is below.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Answer:

Topic 1
If(! ( "A"in window){var=1;} Alert(a);              

Code meaning: If window does not contain property A, declare a variable A and assign a value of 1.

You might think that alert comes out with a result of 1, and then the actual result is "undefined". To understand why, you need to know the 3 concepts in JavaScript.

First, all global variables are properties of the window, and the statement var a = 1; equivalent to WINDOW.A = 1; You can detect if a global variable is declared as follows:

"Variable name" in window 

Second, all the variable declarations are at the top of the range scope and look at similar examples:

Alert("a"in window);  var a;       

At this point, although the declaration is after alert, the alert pops up is still true, because the JavaScript engine first graves all the variable declarations and then moves the variable declarations to the top, and the final code effect is this:

var a;  Alert("a"in window);       

This makes it easy to explain why the alert result is true.

Third, you need to understand that the meaning of the topic is that the variable declaration is advanced, but the variable assignment is not, because this line of code includes the variable declaration and variable assignment.

You can split the statement into the following code:

var a;     //Declaration =1;  Initialize Assignment       

When a variable is declared and assigned together, the JavaScript engine automatically divides it into two parts in order to advance the variable declaration, not to advance the assignment because he has the potential to influence the code to perform unexpected results.

So, after knowing these concepts, looking back at the code of the topic is actually equivalent to:

var a;  If(! ( "A"in window)){=1;} Alert(a);                

In this way, the meaning of the topic is very clear: first declare a, then determine whether a is in existence, if it does not exist on the assignment is 1, it is obvious that a always exists in the window, the assignment statement will never execute, so the result is undefined.

The word in advance seems a bit confusing, as you can read: precompilation.

Topic 2
var=1,=function a(x){&& a(---x) ; }; Alert(a);               

This topic looks more complicated than it really is, and the result of alert is 1; there are still 3 important concepts that we need to know.

First of all, in topic 1 we know that the variable declaration is completed in the execution context, and the second concept is that the function declaration is also advanced, and all function declarations are declared before the code is executed, and the variable

The same as the volume Declaration. To clarify, the function declaration is code such as the following:

function functionname(arg1, arg2) {//body }       

This is not a function, but a function expression, which is equivalent to a variable assignment:

var=function(arg1, arg2) {//Body };      

To clarify, the function expression does not advance, it is equivalent to the usual variable assignment.

The third need to know is that the function declaration overrides the variable declaration, but does not overwrite the variable assignment, in order to explain this, let's look at an example:

function value() {    return1;} var value;  Alert(typeof value);  "Function"               

As soon as the variable declaration is defined below, but the variable value is still function, that is, the function declaration takes precedence over the variable declaration priority, but if the variable value is assigned, the result is completely different:

function value() {    return1;} var=1;  Alert(typeof value);  "Number"                

After the value is assigned, the variable assignment is initialized to overwrite the function declaration.

Back to the topic, this function is actually a well-known function expression, the function expression is not like a function declaration can overwrite the variable declaration, but you can notice that the variable B contains the function expression, and the function expression name is a; A is considered a function declaration, so it is overwritten by the initialization of the variable, that is, if a (–x) is called an error, and other browsers are allowed to call a (–X) inside the function, because at this time a is still a number outside the function. Basically, IE makes a mistake when it calls B (2), but other browsers return undefined.

After understanding the above, the topic should be replaced by a more accurate and understandable code like this:

var=1,=function(x){&& b(--x);}; Alert(a);               

In this way, it is clear to know why alert is always 1.

Topic 3
function a(){return1;} var a;  Alert(a);             

This is a simple topic: the relationship and impact of function declarations and variable declarations, with function declarations of the same name, are not redefined

Topic 4
function B (x Y, A) { Arguments[2] = 10; Alerta}b (1 2, 3                

On this subject, the ECMAsCRIPT 262-3 specification is explained.

The active object is created at the moment of entry into the function context, and it is initialized by the arguments property of the function. The value of the arguments property is the arguments object.

For a specific definition of the arguments object, see here: ECMAScript arguments Object

Topic 5
function a(){ alert(this);} a.  Call(null);             

This topic can be said to be the simplest, but also the most bizarre! On this topic, let's start with 2 concepts.

This question mainly examines the Javascript's this keyword, specifically here:

About the use of the This keyword in the JavaScript language

About A.call (null); According to the ECMASCRIPT262 specification, the call method takes the global object (that is, window) as the value of this if the caller of the first parameter passed in is null or undefined. So, no matter when you pass in NULL, its this is the Global object window, so the topic can be interpreted as the following code:

function a(){ alert(this);} a.  Call(window);             

So the result of the popup is [object Window] is easy to understand.

—————

Summarize:

Although these 5 topics seem a bit biased, but in fact the survey is still the basic concept, only the knowledge of these basic concepts to write high-quality code.

That's roughly what this is about.

This article is from: Http://julying.com/blog, the original address: http://julying.com/blog/so-you-think-you-know-javascript/, thank the original author to share.

Do you really understand JavaScript?

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.