Question 1: if (! (& Quot; a & quot; inwindow) {vara1;} alert (a); Question 2: vara1, bfunctiona (x) {x & amp; a (-- x) ;}; alert (a); Question 3: functiona (x) {returnx * 2;} var... synt Question 1: if (! ("A" in window) {var a = 1;} alert (a); Question 2: var a = 1, B = function a (x) {x & a (-- x) ;}; alert (a); Question 3: function a (x) {return x * 2 ;}var a; alert (); question 4: function B (x, y, a) {arguments [2] = 10; alert (a);} B (1, 2, 3); Question 5: function a () {alert (this);}. call (null); do not use any help tools to calculate the answer. The answer is as follows ............................ Answer: Question 1if (! ("A" in window) {var a = 1;} alert (a); Code meaning: If window does not contain attribute a, declare a variable, then assign a value of 1. You may think that the result of alert is 1, and then the actual result is "undefined ". To understand why, you need to know three concepts in JavaScript. First, all global variables are the properties of window, and the statement var a = 1 is equivalent to window. a = 1; you can use the following method to check whether global variables are declared: "variable name" in window Second, all variable declarations are at the top of the scope, let's take a look at a similar example: alert ("a" in window); var a; at this time, even though the declaration is after alert, the alert pop-up is still true, this is because the JavaScript engine will first scan all the variable declarations and then move these variable declarations to the top. The final code effect is as follows: var a; alert ("a" in window ); this makes it easy to explain why the alert result is true. Third, you need to understand the meaning of this question: 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; // declare a = 1; // initialize the value assignment when the variable declaration and value assignment are used together, the JavaScript engine automatically divides the variable declaration into two parts so that the variable declaration is made in advance and the assignment is not performed in advance because it may affect the code execution to produce unexpected results. So after understanding these concepts, you can look back at the code of the question, which is equivalent to: var a; if (! ("A" in window) {a = 1;} alert (a); in this way, the meaning of the question is very clear: first declare a and then determine whether a exists, if it does not exist, it is assigned a value of 1. Obviously, a will always exist in the window, and this assignment statement will never be executed, so the result is undefined. This word is a bit confusing in advance. You can understand it as: Pre-compilation. Question 2var a = 1, B = function a (x) {x & a (-- x) ;}; alert (a); this question looks more complex than actually, the result of alert is 1; here there are three important concepts that need to be known. First, in question 1, we know that the variable declaration is completed when it enters the execution context. The second concept is that the function declaration is also in advance, all function declarations have been declared before the code is executed, just like the variable declaration. To clarify, the function declaration is the following code: function functionName (arg1, arg2) {// function body} is not a function, but a function expression, which is equivalent to a variable value assignment: var functionName = function (arg1, arg2) {// function body}; to clarify, if the function expression is not in advance, it is equivalent to assigning values to the usual variables. Third, the function declaration will overwrite the variable declaration, but will not overwrite the variable assignment. To explain this, let's look at an example: function value () {return 1 ;}var value; alert (typeof value); // "function" variable declaration is defined below as soon as possible, but the variable value is still a function. That is to say, in this case, the priority of the function declaration is higher than that of the variable declaration, however, if the value of the variable is assigned, the result is completely different: function value () {return 1;} var value = 1; alert (typeof value ); // "number" after the value is assigned, the variable value initialization overwrites the function declaration. Return to the question. This function is actually a famous function expression. The function expression can overwrite the variable declaration like the function declaration, but you can note that variable B contains the function expression, the name of the function expression is a. Different browsers have different processing methods for the term a. in IE, a considers function declaration as a function, so it is overwritten by variable initialization, that is to say, if a (-x) is called, an error will occur, while other browsers allow calling a (-x) within the function, because at this time a is still a number outside the function. Basically, an error occurs when B (2) is called in IE, but undefined is returned in other browsers. After understanding the above content, the question should be replaced with a more accurate and easier-to-understand code like this: var a = 1, B = function (x) {x & B (-- x) ;}; alert (a); in this way, we can clearly understand why alert is always 1. Question 3 function a () {return 1;} var a; alert (a); this question is simple: Relationship between function declaration and variable Declaration and its impact, when a function declaration with the same name is encountered, the question 4 function B (x, y, a) {arguments [2] = 10; alert (a);} B (1, 2, 3). The ECMAsCRIPT 262-3 norms have explanations for this question. An active object is created when the context of the function is entered. It is initialized through the arguments attribute of the function. The value of the arguments attribute is an Arguments object. for the specific definition of the Arguments object, see: ECMAScript arguments object Question 5 function a () {alert (this);}. call (null); this question can be said to be the simplest and most strange! For this question, we will first understand two concepts. This question mainly describes the this keyword of Javascript. For details, refer to the usage of this keyword in Javascript language: www.2cto.com about. call (null); according to ECMAScript262: If the caller of the object passed in by the first parameter is null or undefined, the call method regards the Global Object (that is, window) as the value of this. Therefore, no matter when you pass in null, this is a global Object window. Therefore, this question can be understood as the following code: function a () {alert (this);}. call (window); so the pop-up result is [object Window], which is easy to understand. ----- Conclusion: although these five questions seem to be a bit biased, they actually look at the basic concepts. Only by familiarizing yourself with these basic concepts can we write high-quality code.
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