6

我想我理解为什么变量存在于声明它们的函数之外,因为你正在返回另一个函数:

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'

现在的写法,调用 A() 就像一个吸气剂。

问:如何编写 myFunction 以便调用 A(123) 是一个 setter?

4

6 回答 6

9

尝试以下操作:

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'
于 2013-03-31T23:43:29.847 回答
3

如果你想要 getter 和 setter,你可以做这样的事情,例如:

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"
于 2013-03-31T23:43:22.450 回答
2

应该很简单:

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;
        }
    }
}

由于closure变量位于函数作用域的闭包内,因此您应该能够以与从中读取相同的方式对其进行分配。

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

于 2013-03-31T23:42:13.230 回答
2

另一种选择是使用一个类并定义 getter 和 setter:

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);

演示:http: //jsfiddle.net/louisbros/bMkbE/

于 2013-03-31T23:59:59.177 回答
1

jsFiddle 演示

让您返回的函数接受一个参数。将其用作二传手:

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'
于 2013-04-01T00:02:05.653 回答
0

重新审视这个问题,我发现我可以这样做:

    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>

于 2015-04-29T16:02:30.547 回答