オブジェクトから値を設定および取得するための短い関数を作成しました(>名前でポイントを取得します)が、私のソリューションが本当に賢いのかどうかはわかりません。
このクエリを完成させるためにどのような変更をお勧めしますか?
var points = {}; //global var
function getPoints() {
var args = arguments, name;
// set points or return them!
if (typeof args[0] == 'object') {
name = args[0].name;
points = { name: args[0].points };
} else {
name = args[0];
return points.name;
}
}
//set:
getPoints(name: "John", points: 0) //set points (0)
getPoints(name: "Pamela", points: 2 ) //set points (2)
//return:
getPoints("John") //returns > (0)
getPoints("Pamela") //returns > (2)