2013-03-31 136 views
4

我想我明白爲什麼變量,他們被宣佈在函數之外存在設定的變量,因爲你返回另一個功能:在封閉範圍

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' 

,它的現在寫的,調用的方式()就像一個getter。

問:如何編寫myFunction以便調用A(123)是一個setter?

+0

說「沒有價值」是有點誤導;函數*是*值。 – icktoofay

回答

7

嘗試以下操作:

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' 
+0

謝謝Thorsten!我沒有考慮使用length屬性來確定是否有參數。這很好。 –

+0

使用它你可以將屬性設置爲'null'或'false'。很長一段時間,我沒有真正編碼js,但記得*有東西* :) – hek2mgl

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/

+0

如果'setTo'是'0'會怎麼樣? ;) –

+0

@FelixKling好點,猜我是太懶惰了!更新。 – metadept

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" 
+0

謝謝elclanrs。我查看了JavaScript getters和setter,但我想我想使用jQuery樣式的getter和setter,在沒有參數的情況下調用函數被認爲是getter,並用參數調用它被認爲是setter。 –

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/

1

jsFiddle Demo

讓你的返回函數接受參數。使用它作爲一個二傳手:

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' 
0

重新審視這個問題,我知道我能做到這一點是這樣的:

function outside() { 
 
    \t var result = 'initialized' 
 
    \t return inside 
 
    \t function inside(argVariable) { 
 
    \t \t if(arguments.length) { 
 
    \t \t \t result = argVariable 
 
    \t \t \t return this 
 
    \t \t } else { 
 
    \t \t \t return result 
 
    \t \t } 
 
    \t } 
 
    } 
 
    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>