Several JS pen questions

Source: Internet
Author: User

First, today in the group saw a few students sent a few words about JS Pen test, take to study, although they have seen the high-level program design, also read the JavaScript language essence, since the JS still have some understanding, but really encountered problems, found and can not be mastered, master not solid. Take this opportunity to review and learn together

Second, a few small questions (write out the contents of the following several questions pop-up box)

(1)

1 if inch window)) {2   var a=1; 3 }4 alert (a);

Parsing: The normal idea: first you need to understand the concept of function declaration promotion, before executing the code, will read the declaration of the function or variable. So when the IF statement determines if a is present in window, the answer is: yes. That is, if the judgment condition is not true, then the code in the if is not executed. When executing to the alert statement, the result is undefine!, since a only makes a declaration without assigning a value The actual answer is: 1. Now , do not question yourself, your thinking is very correct, just miss a little detail, if the judgment is "a", is a string, and we declare the assignment is a variable, not a Oh ~ because the string A is not defined, so does not exist in the Window object ~

(2)

1 function A (x) {2   return x*2; 3 }4var  A; 5 alert (a);

Parsing: We refer to the concept of function declaration promotion in the previous question. In this case there will be two declaration ascension, and I personally understand that two declarations are the same, all a, that is, a variable declared two times, can be regarded as the same. The function is called when executing the code, so the final answer is "alert" a.

Deformation: What happens if var a is replaced with var a=1 ?

(3)

1 var a=1; 2 b=function  A (x) {3   x&&a (--x); 4 }; 5 alert (a);

Parsing: Do not need to see the internal implementation of the function, we first look at the structure of the function. A variable A, a function expression B, the answer is naturally b. At first I was also puzzled, after trying to find that the form of a function expression in a does not occur function declaration promotion, and B can only be accessed inside the function, cannot be accessed externally. It concludes that it is not meaningful to add identifiers to anonymous functions of function expressions.

(4)

1 function A () {2   alert (this); 3 }4  a.call (null);

Analysis: There are two problems to be noted in solving this problem: 1, the use of Call 2, the particularity of NULL; the use of call and apply is the method of hijacking another object, inheriting another object's properties, but their parameters are different, not involved in here, interested children shoes can go to cram their own ~ In this problem, the execution context of the A function is changed to the object in parentheses. Normally, the object in parentheses should be output, but null and undefined are special, ECMAScript5, when the parameters of call are null and undefined, the execution context is global, in the browser window is the global object, So the alert is out of window.

Third, summary: personal ideas, welcome everyone to put forward opinions and different views, study together ~ ~ ~

Several JS pen questions

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.