JavaScript face question Collection

Source: Internet
Author: User

One, closed package

1.

1 functionF1 () {2       varn=999;3Nadd=function() {N+=1}4       functionF2 () {5 alert (n);6 }7       returnF2;8 }9     varresult=F1 ();TenResult ();//999 One Nadd (); AResult ();// +

The first thing to say is that closures are the core concept inside functional language.
When a higher order nesting function is present, the compiler does it closure convention闭包变换 , and the core is that the variable is not allocated on the stack, but is allocated on the heap. That's why F1 has returned, but N can still be +1.
The landlord gives this program, is actually a high-order nesting function.
1. Because there are defined functions inside the function, this is nested. Pascal also allows nested functions.
2. The reason for the higher order is that functions can be called parameters passed and returned, like the C language we are familiar with.
But when higher order and nesting occur simultaneously, it can cause trouble, so both Pascal and C can only support one of them.

Let me analyze the execution flow of this program.
1. var result=f1 (); A function f2 is returned, so result is F2. This high-order function features a reference to the C-language function pointer.
2. result (); Call F2, apparently output 999.
3. Nadd (); It is important to note that this nadd is actually a lambda at the time of definition, an anonymous function, and the function is n+=1. This function is assigned to NADD when defined. So at this point, the n+=1 is actually called. Why can I find n? Because n is inside the heap.
4. Result (); Call F2, apparently output 1000.

Finally, how n is destroyed on the heap, this job is the garbage collector responsible. When n is not referenced by an env in any closure, it is recycled.

SF Reference Link: http://segmentfault.com/q/1010000002880935?_ea=223108

Second, the variable promotion

1.

1 varx= 0;2 varf=function(){3X=1;4 };5 f ();6alert (x);//17 functionf () {8x = 2;9 }Tenf ();//1 Onealert (x);

Equivalent code:

var x; var F; function f () {    x=2;} x=0; F=function() {    x=1;}; f (); alert (x); // 1f ();  // 1alert (x); // 1

function expression does not exist declaration in advance

sf:http://segmentfault.com/q/1010000002886791/a-1020000002886944 http://segmentfault.com/q/1010000002883076

JavaScript face question Collection

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.