0
Higher Order functions in JavaScript
Here is the code snippet: //start of code function greaterThan(n){ //line1 return function(m){return m > n;}; //line2 } //line3 var greaterThan10 = greaterThan(10); //line4 console.log(greaterThan10(11)); //line5 //end of code My question is, the function inside the function greaterThan needs an argument 'm' but I don't see any argument being passed. How does this code work in general?
5 Réponses
+ 7
Your above code is a simple closure. Read from here to get more insight:
https://www.sololearn.com/discuss/344271/?ref=app
0
line 4 defines a function with const value n=10.
line 5 now calls this function greaterThan10 with argument m=11. now you compare in line 2 the values (m=11) > (n=10)
0
@Volker I don't quite follow what you said, but I was go through it again and again and here is what I have made myself understand, please enlighten me if I am wrong.
Line 4 calls a function 'greaterThan' with argument as 10, and stores the whole function in variable 'greaterThan10', so when in line 5 'greaterThan10(11)' is called, the argument 11 is passed on as 'm'; since the whole function was stored in variable 'greaterThan10'
Is my interpretation correct?
0
yes that's the behaviour
0
thank you so much for your reply and time Mr. Volker :)