Closure is function in inner a function.
>> function addTwoNumber(n){
>> function add(m){
>> return n+m;
>> }
>> return add;
>> }
And you use like this one:
>> var useN = addTwoNumber(2);
if you display the variable useN, you will get
>> function add(m) { return n+m; }
So you need accessing above inner function by using bracket:
>> var useN = addTwoNumber(2)(3);
or
>> var useN = addTwoNumber(2);
>> useN(3);
The above will result in "5".
No comments:
Post a Comment