0

Is this a closure in JavaScript?

var test = function(b){
    var a = 1;
    return function(b){
        a + b
    }
};
var c = test(2);
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
Hello-World
  • 9,277
  • 23
  • 88
  • 154
  • Please see http://stackoverflow.com/questions/111102/how-do-javascript-closures-work – topcat3 Feb 28 '13 at 14:42
  • 4
    Yes, the returned function is a closure. It's kind-of strange, because the parameter to the "test" function isn't used. – Pointy Feb 28 '13 at 14:42
  • 3
    As @Pointy mentioned, this will **not** return `3`. This will return the anonymous function `function(b) { a + b }` (which doesn't actually do anything, as it doesn't return anything). – h2ooooooo Feb 28 '13 at 14:44

4 Answers4

2

A closure is introduced then you define a function within test that returns local properties of the test function. an example of a closure would be here:

;(function() {

    var local = 123

    window.foo = function() {
        return local
    }
})()

What you're pretty close to in your example is currying, which involves a function that returns a function to take a second parameter. e.g:

function add(a) {
    return function(b) {
        return a + b;
    }
}

add(5)(6) // 11
Luke Channings
  • 1,053
  • 9
  • 18
0

var a; and parameter b of the outer function are part of the closure of the inner function. For detailed explanation of closures have a look in the FAQ

var test = function(b){ // <= param b is part of the closure of the inner function
    var a = 1;          // <= a is part of the closure of the inner function as well 
    return function(b){ // <= the inner function
        a + b           // <= here you are just add ind a and b together
                        //    return a + b; //would be more appropriate
    }
};
var c = test(2);
Community
  • 1
  • 1
Uwe Günther
  • 2,971
  • 2
  • 21
  • 26
0

var globalVar = "xyz";

(function outerFunc(outerArg) {
    var outerVar = 'a';
    
    (function innerFunc(innerArg) {
    var innerVar = 'b';
    
    console.log(
        "outerArg = " + outerArg + "\n" +
        "innerArg = " + innerArg + "\n" +
        "outerVar = " + outerVar + "\n" +
        "innerVar = " + innerVar + "\n" +
        "globalVar = " + globalVar);
    
    })(456);
})(123);
this code is same to above code but is new way of writing result and output will be same })(123); here firstly automatically call outer function pass argument 123 then })(456); call inner function automatically

Note in this way no return require

Avinash Maurya
  • 332
  • 3
  • 4
  • Whenever we use function inside another function, a closure is used. means A closure is basically when an inner function has access to variables outside of its scope – Avinash Maurya Sep 12 '18 at 02:49
0

var globalVar = "xyz";

function outerFunc(outerArg) {
    var outerVar = 'a';
    
     var r3= function innerFunc(innerArg) {
    var innerVar = 'b';
    
    console.log(
        "outerArg = " + outerArg + "\n" +
        "innerArg = " + innerArg + "\n" +
        "outerVar = " + outerVar + "\n" +
        "innerVar = " + innerVar + "\n" +
        "globalVar = " + globalVar);
    
    };
 return r3;
};

var r=outerFunc(123);
r(456);

var r=outerFunc(123); ...here we have called outer-function and assign this result return in r variable we cannot use inner function directly. we have to call outer function and assign their return in varible because above code return function

Avinash Maurya
  • 332
  • 3
  • 4
  • above code we can also write this simple way old format we have use inner and outer function inner function we have pass in a variable var r3 which is call when out-function call then as return of outer function is inner function – Avinash Maurya Sep 12 '18 at 02:52