Setting a variable in the closing area

I think I understand why variables exist outside the function in which they were declared, because you are 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 it is written now calls A () as a getter.

Q: How can I write myFunction so that the call to A (123) is setter?

+4
source share
6 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' 
+7
source

You can 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" 
+3
source

It 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 scope of the function, you should be able to assign it the same way you can read it.

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

+2
source

Another alternative would be to use a class and define getters and setters:

 function MyClass(p){ this._prop = p; } MyClass.prototype = { constructor: MyClass, get prop(){ return this._prop; }, set prop(p){ this._prop = p; } } var myObject = new MyClass("TEST"); console.log(myObject.prop); myObject.prop = "test"; console.log(myObject.prop); 

Demo: http://jsfiddle.net/louisbros/bMkbE/

+2
source

jsFiddle Demo

Ask your returned function to accept an argument. Use it as a setter:

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

Repeating this question, I see that I can do it as follows:

  function outside() { var result = 'initialized' return inside function inside(argVariable) { if(arguments.length) { result = argVariable return this } else { return result } } } myFunction = outside() // outside returns a function X = myFunction() // returns: 'initialized' $('body').append(X + '<br>') myFunction(123) // setter X = myFunction() // returns: 123 $('body').append(X) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
0
source

All Articles