我创建了一个简短的函数来设置和检索对象的值(> 按名称获取点),但我不确定我的解决方案是否真的那么聪明。
您建议进行哪些修改来完善此查询?
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)