Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain me the flow of this JavaScript function? (Closure concept)

I'm reading "Eloquent JavaScript". Chapter 3 introduces "Closure" concept and gives you a couple of examples. One of these is next one:

function multiplier(factor) {
    return function(number) {
        return number * factor;
    };
}

var twice = multiplier(2);
console.log(twice(5));
// → 10

I think I understood the concept. If first I execute console.log(twice), since variable number is undefined, what I get is [Function]. What I don't understand is how twice(5) works. Why local variable number is initialized with value 5?

Also, why if I execute console.log(multiplier(2,5)) I don't get 10 as a result?

Thanks.

like image 561
Noob_Number_1 Avatar asked Aug 03 '16 09:08

Noob_Number_1


People also ask

How will you explain closures in JavaScript?

A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function's scope from an inner function.

How do you explain what closure is?

Definition of closure 1 : an act of closing : the condition of being closed closure of the eyelids business closures the closure of the factory. 2 : an often comforting or satisfying sense of finality victims needing closure also : something (such as a satisfying ending) that provides such a sense.

What is closure in JavaScript with real time example?

In JavaScript, closures are defined as inner functions that have access to variables and parameters of outer function even after the outer function has returned.

What is closure in JavaScript interview questions?

A closure is a function that accesses variables “outside” itself, in another function's scope, even after the parent function has closed. A closure is able to do this because of a saved reference to the lexical environment it was initially defined in.


1 Answers

Because multiplier returns a function, so twice is equal to the returned function, NOT the multiplier function.

However, when multiplier is called the factor variable is passed and used within the returned function.

So to make it easier to understand, consider that twice is basically:

var twice = function(number) {
    return number * 2;
};

Where factor has been replaced by the value you passed in when calling multiplier(2).


I think I understood the concept. If first I execute console.log(twice), since variable number is undefined, what I get is [Function].

When you use console.log(twice) you are not actually calling the function twice, you are simply logging the value of it. So the output of [Function] is not because number is undefined, it is because you are outputting the actual function rather than the result of it.


Also, why if I execute console.log(multiplier(2,5)) I don't get 10 as a result?

Here you are calling the multiplier by providing 2 arguments, though you have only defined the function to accept one parameter (factor). In javascript, this won't cause an error, but you will just get the first value mapped in factor (factor = 2).

Note: There are ways to still access all the supplied arguments even if you don't have parameters defined for them (here's an example)

Something that would be possible to get a result of 10 which might be of interest is to use the following code:

var result = multiplier(2)(5); // result = 10
like image 134
musefan Avatar answered Oct 19 '22 01:10

musefan