JS interview questions, waiting for a boss to show up to solve my brother's doubts.

the correct answer is 0 br 8 limel 14 lime14 < mai > execute fn (1), global iTunes 4, private nasty 1.
executes f (2), global iTunes 0, private nouns 0.

< H1 > executes fn (3) (4), and the global iTunel 8 (where n I use in the calculation of the next code, which is followed by 2, and results in a discrepancy with the correct answer, if calculated with 2, the next execution I is 0). < / H1 >

execute f (5), global iTunel 14 {what puzzles me is why the n in this code is not the n after the execution of fn (3) (4), but n}
console.log (I) / /-14

in fn (1).
      let i=2
        let fn=function(n){
            i*=2
            return function(m){
                i-=(n--)+(PPm)
                console.log(i)
            }
        }
        let f=fn(1)
        f(2)
        fn(3)(4)
        f(5)
        console.log(i)


Jun.07,2022

each execution of fn returns a new anonymous function your fn (1) the returned function has been cached with a f variable, so the following call f (2) f (5) in n is 1
fn (3) (4) first execute fn (3) Counting the anonymous function returned by this anonymous function and fn (1) is the same, but it is actually two anonymous functions

.

the value of f (2) n the first time you execute has become 0
the second time f (5) I =-8; m = 5; n = 0
so the value of iMelt = (nList -) + (PPm) is -14

.

is not a closure


after understanding closures, the difference between PPn and nPP, this problem is an arithmetic problem

you need to be serious (I am not serious enough for the old driver, resulting in flipping out of the car?)


my understanding is that when you execute let f=fn (1) , you have formed a closure, n becomes a local variable in this closure, and then executing f (3) only forms another closure, which has nothing to do with f , so n is not the n after the execution of fn (3) (4), but the n in fn (1).


good question. It took a long time to figure out where did


f come from? It is obtained by executing fn (1) and has nothing to do with fn (3) (4).


these are two separate closures. The problem is not difficult, but it needs to be calculated more carefully

Menu