我有一個函數,它有一些變量,因爲它們在函數內部,它們是局部函數,因此是私有的。現在我可能有很多這樣的變量,爲了設置一個值並從外部獲取變量值,我必須編寫set並獲取每個私有變量。 反正在javascript中有獲取和設置所有私有變量(PrivateOne和PrivateTwo)的常用功能。實現一個函數來設置並獲取JavaScript中的所有私有變量的值
privateClosure = (function(){
var PrivateOne;
var PrivateTwo;
setPrivate = function(VariableName,Value)
{
VariableName = Value;
}
getPrivate = function(VariableName)
{
return VariableName;
}
return{setPrivate,getPrivate};
})();
// I want to do something like this
privateClosure.setPrivate(PrivateOne,10);
privateClosure.getPrivate(PrivateOne);
privateClosure.setPrivate(PrivateTwo,20);
privateClosure.getPrivate(PrivateTwo);
//Only one Function for all the private variable in closure.
Is there anyway in javascript to have common function for getting and setting all private variable(PrivateOne and PrivateTwo).
從你的問題中不清楚你到底想要完成什麼。 –
@TanveerBadar我已經更新了問題的背景。 –