Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a variable in the closure scope

I think I understand why variables exist outside of the function they were declared in, because you're returning another function:

myFunction = function() {
    var closure = 'closure scope'
    return function() {
        return closure;
    }
}
A = myFunction(); // myFunction returns a function, not a value
B = A(); // A is a function, which when run, returns:
console.log(B); // 'closure scope'

The way that it's written now, calling A() is like a getter.

Q: How can I write myFunction so that calling A(123) is a setter?

like image 639
Phillip Senn Avatar asked Mar 31 '13 23:03

Phillip Senn


People also ask

What is a closure variable?

A closure is the combination of a function and the lexical environment within which that function was declared. This environment consists of any local variables that were in-scope at the time the closure was created.

Which two methods of defining a variable has block scope?

Block scope is defined with curly braces. It is separated by { and } . Variables declared with let and const can have block scope. They can only be accessed in the block in which they are defined.

Where are closure variables stored?

The Complete Full-Stack JavaScript Course! A closure function gives access to the scope of an outer function from an inner function. It also allows private variables. Closure variables are stored in stack and heap.

Which variables have a function scope?

Local variables have Function Scope: They can only be accessed from within the function. Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.


3 Answers

Try the following:

myFunction = function() {
    var closure = 'closure scope'

    // value is optional
    return function(value) {
        // if it will be omitted
        if(arguments.length == 0) {
            // the method is a getter
            return closure;
        } else {
            // otherwise a setter
            closure = value;
            // with fluid interface ;)
            return this;
        }
    }
}
A = myFunction(); // myFunction returns a function, not a value
A(123); // set value
B = A(); // A is a function, which when run, returns:
console.log(B); // '123'
like image 151
hek2mgl Avatar answered Sep 19 '22 10:09

hek2mgl


You could do something like this if you want both getter and setter for example:

var func = function() {
  var closure = 'foo';

  return {
    get: function() { return closure; },
    set: function(value) { closure = value; }
  }
};

var A = func();

A.set('foobar');
console.log(A.get()); //=> "foobar"
like image 45
elclanrs Avatar answered Sep 21 '22 10:09

elclanrs


Should be as simple as:

myFunction = function() {
    var closure = 'closure scope'
    return function(setTo) {
        if (typeof setTo !== "undefined") {
            closure = setTo;
            return this; //support call chaining, good idea hek2mgl
        } else {
            return closure;
        }
    }
}

Since the closure variable is within the closure of the function's scope, you should be able to assign to it the same way you can read from it.

See jsFiddle: http://jsfiddle.net/WF4VT/1/

like image 22
metadept Avatar answered Sep 22 '22 10:09

metadept