JavaScript Tutorial: A few questions about deep understanding of JS

Source: Internet
Author: User
Tags expression variables variable window

Read the Baranovskiy written by Dmitry So, I think you know JavaScript, and also on the Nicholas C. Zakas reply , The understanding of JavaScript has come a step further, trying to record your understanding as follows
First question:

daimaren.cn©2009-2010 by Tomie Zhang
if (! () A "in Window") {
    var a = 1;
}
alert (a);

This is a study of JS variables, the test translated into a natural language to the effect is: if the window does not exist a property, then define a variable and assign it equal to 1. The expected value here is 1, but unfortunately it will pop up "undefined", which is why? First, the global variables for JavaScript are the properties of Windows, and you can use the form:

daimaren.cn©2009-2010 by Tomie Zhang
Alert ("daimaren.cn" in Winodw)

To see that var a = 1 is equivalent to WINDOW.A = 1, followed by the JavaScript engine when scanning code to run the variable ahead of scope, such as:

daimaren.cn©2009-2010 by Tomie Zhang
Alert ("A" in window);
var A;

In fact, after parsing is

daimaren.cn©2009-2010 by Tomie Zhang
var A;
Alert ("A" in window);

So its result is true, and finally the declaration of JavaScript is separate from the initialization,

daimaren.cn©2009-2010 by Tomie Zhang
var a = 1;
/* In fact, the runtime will become * *
var A;
A=1;

JavaScript automatically splits these two steps, and Nicholas's explanation is that the split allows the declaration to be placed above the scope, and why, because the declaration initializes the variable, which can affect the value of the code run-time variable, resulting in unexpected results.
So, knowing this, the first test will actually run like this:

daimaren.cn©2009-2010 by Tomie Zhang
var a;//a variable that is automatically run, without initializing the
if (! A "in Window") {//flase
    var a = 1;
}
alert (a);//Eject undefined

Second question:

daimaren.cn©2009-2010 by Tomie Zhang
var a = 1,
b = function A (x) {
        x && a (--x);
};
alert (a);

If you understand the first question, then the problem is well solved, this is about function, it has 2 kinds, one is function declaration, one is functional expression (function expression), in the form of:

daimaren.cn©2009-2010 by Tomie Zhang
Functions Daimaren () {//This is a function declaration do anything your want here ...
}
var daimaren = function () {//This is a function expression do
  anything your want here ...
}

All function declarations run at run time, and function expressions do not run the following two-paragraph code:

daimaren.cn©2009-2010 by Tomie Zhang
What does foo ()//will pop up?
function foo () {
	alert (' I am a function function declaration from daimaren.cn ');
}
var foo = function () {
	alert ("I am a function function expression from daimaren.cn");
daimaren.cn©2009-2010 by Tomie Zhang
function foo () {
	alert (' I am a function function declaration from daimaren.cn ');
}
var foo = function () {
	alert ("I am a function function expression from daimaren.cn");
Foo ();//What will pop up?

With these two examples, we can be sure that the answer to the second question will always be 1.
Question number three.

daimaren.cn©2009-2010 by Tomie Zhang
function A (x) {return
    x * 2;
}
var A;
alert (a);

If you understand the previous concept of variable predecessors, well, that's a good idea. The only difference is that the precedence of the function declaration is higher than the normal variable declaration unless the variable declaration is initialized, that is, the above code will first perform function A and ignore Var a, and consider the following:

daimaren.cn©2009-2010 by Tomie Zhang
function A (x) {return
    x * 2;
}
var a = "1";
alert (a);

Question fourth.

daimaren.cn©2009-2010 by Tomie Zhang
function b (x, Y, a) {
    arguments[2] = ten;
    alert (a);
}
B (1, 2, 3);

This is well understood, the operation of the value of 10, here is mainly the understanding of arguments, ECMA-262 the third edition of the 10.1.8 chapter of the arguments explained as follows:

For each non-negative integer, arg, less than the value of the ' length ', a ' is ' created with name ToString (a RG) and property attributes {Dontenum}. The initial value of this property is the value of the corresponding actual parameter supplied by the caller. The actual parameter value corresponds to ARG = 0, the second to arg = 1, and. In the case when you are less than the number of formal parameters for the Function object, which shares its value The corresponding property of the activation object. This means is changing this property changes the corresponding property of the activation object and Vice.

The Parameter object is equivalent to the corresponding parameter established copy, the parameter changes it will also follow the change, their correspondence, for example in the above function Arguments[0] corresponds to the x,arguments[1 corresponding y,arguments[2] corresponds to a, It seems to be an array but when you typof, you'll find them object. The following example is understood in relation to their correspondence:

daimaren.cn©2009-2010 by Tomie Zhang
function b (x, Y, a) {
    arguments[2] = x;
    alert (a);/expected to be 3? But a fancy sentence, has been pointed to X, so is 1
}
B (1,2,3);

Question fifth.

daimaren.cn©2009-2010 by Tomie Zhang
function A () {
    alert (this);
}
A.call (NULL);

This is called (I have a headache, embarrassed, but the master said is the simplest of the 5 topics), look at. First, to figure out what the this pointer is pointing to, Nicholas Example:

daimaren.cn©2009-2010 by Tomie Zhang
var o= {
    method:function () {
        alert (this = = O);    True
    }
}
O.method ();

When method is called, TIHS points to O because it is a way under O, its context is in O, and if it is not in the object of O, it will point to window:

daimaren.cn©2009-2010 by Tomie Zhang
    Function method () {
        alert (this = = window);    True
    }
O.method ();

OK, then you need to know what call is for, saying that call is a fantastic way, and it's easy to confuse, official explanation:

Call method
Please see
Apply to: Function Object
Requirements
Version 5.5
Invokes one of the object's methods to replace the current object with another object.

Call ([thisobj[,arg1[, arg2[, [,. argn]]]]
Parameters
Thisobj
Options available. The object that will be used as the current object.
Arg1, Arg2, argn.
Options available. A sequence of method parameters is passed.
Description
The call method can be used to invoke a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by Thisobj.

If the thisobj parameter is not supplied, the Global object is used as a thisobj.

The official explanation is easily confusing, and Nicholas's explanation for the big wet is as follows:

The call () method executes a function as if it were a method of another object

Well, let's look at an example:

daimaren.cn©2009-2010 by Tomie Zhang
function Add (a,b)
{
    alert (a+b);
}
function sub (a,b)
{
    alert (a-b);
}
Add.call (sub,3,1); 4

This is actually replacing the sub method with the Add method and passing in the parameter values, equivalent to the

daimaren.cn©2009-2010 by Tomie Zhang
Add.call (sub,3,1) = = Add (3,1)

Well, understanding the this pointer and call, and looking back at the topic, it's actually equivalent to:

daimaren.cn©2009-2010 by Tomie Zhang
function A () {
    alert (this);
}
A (null);

This is a function declaration that is not affected by any effect, and a function declaration is an object, so the result is obvious.
This is a half-translation plus half of their own understanding, if there are errors also please advise .... In addition, Nicholas Master said some people like to take such a topic to face the question, he felt that no respect for the interviewer is not useful, because these errors or points are not normal work can be met, just as the interviewer and let people explain the principle of jet power ... This is very agree, academic research can be, if take to deliberately difficult people is a bit bad.



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.