Home Web Front-end JS Tutorial Analysis of a JS front-end closure interview question_javascript skills

Analysis of a JS front-end closure interview question_javascript skills

May 16, 2016 pm 03:23 PM
js Closure

Question

Code A

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

function fun(n,o){

  console.log(o);

  return {

    fun:function(m){//[2]

      return fun(m,n);//[1]

    }

  }

}

 

var a=fun(0);

a.fun(1);

a.fun(2);

a.fun(3);

var b=fun(0).fun(1).fun(2).fun(3);

var c=fun(0).fun(1);

c.fun(2);

c.fun(3);

Copy after login

Find the program output

This is a closure test question

Convert to equivalent code

The fun attribute of the object returned by return corresponds to a newly created function object. This function object will form a closure scope, allowing it to access the variable n of the outer function and the outer function fun. In order not to combine the fun function with The fun attribute is confused, we modify the above code as follows:
Code B

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

function _fun_(n,o){

  console.log(o);

  return {

    fun:function(m){

      return _fun_(m,n);

    }

  }

}

 

var a=_fun_(0);//undefined

a.fun(1);//0

a.fun(2);//0

a.fun(3);//0

 

var b=_fun_(0).fun(1).fun(2).fun(3);

//undefined,0,1,2

 

var c=fun(0).fun(1);//undefined,0,

c.fun(2);//1

c.fun(3); //1

Copy after login

Then some students asked, why can it be changed like this? How can you be sure that the fun at [1] is not the fun at [2] where the code is located? You must know that the fun attribute here points to a function object. ~
Here we talk about the lexical scope of JS. The JS variable scope exists in the function body, that is, the function body, and the scope of the variable is determined when the function definition is declared, not when the function is run.
The following code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

var name="global";

function foo(){

  console.log(name);

}

 

function fooOuter1(){

  var name="local";

  foo();

}

fooOuter1();//输出global 而不是local,并且和闭包没有任何关系

 

function fooOuter2(){

  var name="local";

  function foo(){

    console.log(name);

  }

  foo();

}

fooOuter2();//输出local 而不是global,在函数声明是name变量作用域就在其外层函数中,嗯嗯就是闭包~<br />

Copy after login

Okay, let’s go back to the topic. In the function declaration definition stage, the anonymous function at [2] is defined and declared. It is found that a function object named fun needs to be referenced at [1]. Then first find it in the current function body. , if you find it is not there, then go to its outer function - the wrapping function of this anonymous function to find it, and you find that it is not there either. Go to the outer function and find that there is no function wrapper outside, then go to the global environment to find it, eh. Finally found it... Just specify the fun function as the fun function object in the global environment and add it to the closure of the anonymous function. At this point we know why code B is equivalent to code A~~~

Create closure scope

After the lexical analysis, JS determines a closure, which is the closure of the anonymous function corresponding to the fun attribute of the returned object - the function internal variable n that accesses _func_ in the global environment and its outer function;
Every time _func_ is executed, the scope information of the variables in the closure will be passed to the function execution environment for use when obtaining the variable value when the function is executed

Execution output

1

2

3

4

var a=_fun_(0);//undefined

a.fun(1);//0

a.fun(2);//0

a.fun(3);//0

Copy after login

_fun_ function is executed, because the second parameter is undefined, the output is undefined. Then return an object with the fun attribute pointing to a function object - with a closure, able to access _fun_ and the variable n_
a.fun(1) executes the fun method of the returned object, passes in the value of m 1, and the call returns _fun_(1,0)
So the output is 0, a.fun(2), a.fun(3) and a.fun(1)

var b=_fun_(0).fun(1).fun(2).fun(3);
Equivalent code:

var b=_fun_(0);
var b1=b.fun(1);
var b2=b1.fun(2);//[3]
var b3=b2.fun(3);//[4]
The first two sentences are the same as the above output undefined,0. When [3] is called, there is a closure in the b1 object, which references the _fun_ function and the outer function variable n=1, so The function call performed by the anonymous function is _fun_(2,1), the output result is 1, and a new object is returned.
When [4] is executed, the b2 object also has a closure, which refers to the _fun_ function and the outer function variable n=2. When _fun_(3,2) is executed, the output result is 2

1

2

3

var c=fun(0).fun(1);//undefined,0,

c.fun(2);//1

c.fun(3); //1

Copy after login

If you can understand the previous code execution explanation and understand the above code execution output, there will be no problem. I hope you like it.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Recommended: Excellent JS open source face detection and recognition project

What is the meaning of closure in C++ lambda expression? What is the meaning of closure in C++ lambda expression? Apr 17, 2024 pm 06:15 PM

What is the meaning of closure in C++ lambda expression?

How to implement closure in C++ Lambda expression? How to implement closure in C++ Lambda expression? Jun 01, 2024 pm 05:50 PM

How to implement closure in C++ Lambda expression?

What are the advantages and disadvantages of closures in C++ functions? What are the advantages and disadvantages of closures in C++ functions? Apr 25, 2024 pm 01:33 PM

What are the advantages and disadvantages of closures in C++ functions?

The impact of function pointers and closures on Golang performance The impact of function pointers and closures on Golang performance Apr 15, 2024 am 10:36 AM

The impact of function pointers and closures on Golang performance

Solve the memory leak problem caused by closures Solve the memory leak problem caused by closures Feb 18, 2024 pm 03:20 PM

Solve the memory leak problem caused by closures

The relationship between js and vue The relationship between js and vue Mar 11, 2024 pm 05:21 PM

The relationship between js and vue

How are closures implemented in Java? How are closures implemented in Java? May 03, 2024 pm 12:48 PM

How are closures implemented in Java?

See all articles