JavaScript Common face questions

Source: Internet
Author: User
Tags closure

Closure related interview questions:
1.
var a=0,b=0; function A (a) {a=function(b) {Console.log (a+b++);}; Console.log (a);} A (1); A (12);

A (1) normal execution, Console.log (a);//1 when a function is called, a global a function is created, overwriting the original a function, but a is protected as a protected variable to form a closure

When function a changes to

A:function (b) {(a=1)
Console.log (a+b++);
// };
(a=1) as a protected variable
When a (12) is called again , the Console.log (a+b++) is returned, and the//1+12 result is 13;

2.
1 functionFun (n,o) {2Console.log (o);//output A second parameter each time3   return{//returns an object4Funfunction(m) {//The object contains the function5       returnFun (m,n);//the first parameter of the outer function is protected within the function6     }7   }8 }9 varA=fun (0);TenA.fun (1); OneA.fun (2); AA.fun (3);
var b=fun (0). Fun (1). Fun (2). Fun (3);
var c=fun (0). Fun (1); C.fun (2); C.fun (3);

Fun () function The main line of the problem: output the second parameter each time, and create a closure package the first parameter, the closure of the variable will automatically become the second parameter of the next call

Here's a closer look at the question: Var a=fun (0);

Console.log (o);//undefined

A:{fun:function (M) {(n=0) return Fun (M,n)}} at this time n=0 as a protected variable encapsulated in the closure

A.fun (1);//fun (1,0); Console.log (0);//0

A.fun (2);//fun (2,0); Console.log (0);//0

A.fun (3);//fun (3,0); Console.log (0);//0

For

var b=fun (0). Fun (1). Fun (2). Fun (3);
The specific analysis is as follows:
var b=fun (0)//undefined
//{fun (M) {(n=0) return fun (M,n);}}
. Fun (1)//0
//{fun (M) {(n=1) return fun (M,n);}}
. Fun (2)//1
//{fun (M) {(n=2) return fun (M,n);}}
. Fun (3);//2
B: {fun (M) {(n=3) return fun (M,n);}}

For

var c=fun (0). Fun (1); C.fun (2); C.fun (3);
The specific analysis is as follows:
var c=fun (0)//undefined
. Fun (1);//0
C: {Fun (M) {(n=1) return Fun (M,n)}}
C.fun (2);//1
C.fun (3);//1

1 varFuns= (function(){2    for(vari=0,arr=[];i<3;i++){3arr[i]=function() {Console.log (i)};4}//i=35   returnarr;6 })();7 ////funs:[(i=3)8 ////function () {Console.log (i)},9 ////function () {Console.log (i)},Ten ////function () {Console.log (i)} One //// ] AFuns[0] ();//3 -FUNS[1] ();//3 -FUNS[2] ();//3

This topic mainly examines the loop creation function, and does not call the function, so the

function() {Console.log (i)} is placed in the array funs, and the Console.log is executed when the functions in funs are called, at which point  
So all three results are output 3.

JavaScript Common face questions

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.