Do you really understand JavaScript?

Source: Internet
Author: User

Topic

Topic One:

if (! (" A "in window) {var a = 1;} alert (a); topic two: var a = 1,b = function A (x) {x && a (--x);}; alert (a); Title III: Function A (x) {return x * 2;} var A;alert (a); topic Four: Function B (x, Y, a) {arguments[2] = 10;alert (a);} B (1, 2, 3); topic Five: Function A () {alert (this);} A.call (null); Please do not use any help tool, mental arithmetic answer. The answer is below. ..... Answer: Title 1if (! (" A "in window) {var a = 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 pop-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; Statement A = 1; When initializing an assignment worth variable declaration and assignment, the JavaScript engine automatically divides it into two parts in order to advance the variable declaration, not to advance the assigned step because he has the potential to affect the code's execution of unexpected results. So, after knowing these concepts, looking back at the topic code, in fact, is equivalent to: Var a;if (! A "in window)) {a = 1;} alert (a); Thus, the meaning of the topic is very clear: first declare a, thenTo determine if a is present, and if it does not exist, the assignment is 1, it is obvious that a is always present in the window, and the assignment statement is never executed, so the result is undefined. The word in advance seems a bit confusing, as you can read: precompilation. Topic 2var A = 1,b = function A (x) {x && a (--x);}; alert (a); This topic looks more complicated than it really is, 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 in advance, and all function declarations have been declared before executing the code, as are the variable declarations. To clarify, a function declaration is a code such as functions functionname (Arg1, arg2) {//function body} is not a function, but a function expression, which is equivalent to a variable assignment: var functionname = function (Arg1, ARG2) {//function body}; clarify that 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 () {return 1;} 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 precedence of the variable declaration, but if the variable value is assigned, the result is completely different: function Value () {return 1;} var value = 1;alert (typeof value); "Number" After the value assignment, the variable assignment initialization overrides 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 A = 1,b = function (x) {x && B (--x);}; alert (a); In this way, it is clear to know why alert is always 1. Title 3function A () {return 1;} var A;alert (a); This topic is relatively simple: the relationship and impact of function declaration and variable declaration, encountering a function declaration of the same name, will not redefine the title 4function B (x, Y, a) {arguments[2] = 10;alert (a);} B (1, 2, 3); On this topic, 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 arguments object, see here: ECMAScript arguments object title 5function 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 focuses on JavaScript's this keyword, specifically here: about the use of the This keyword in the JavaScript language www.2cto.com 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. ————— Summary: 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. How to make your own JS more perfect

1: Style separated from JavaScript

2:1, the Length property in the loop statement can be saved with a variable. 2. Create a reusable function for the event handler.

3: Use configuration objects to hold hard-coded code, such as the text label used or the custom property name. facilitates follow-up maintenance.

4: Names that have meaning for variables and functions

5: Add the necessary notes

Do you really understand JavaScript?

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.