i’ve created a short function to set and retrieve values from a object (> get points by name), but I’m not sure if my solution is that really smart.
What modifications do you recommend to perfect this query?
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)
[edit] previous answer was wrong: didn’t mention the impossibility of
getPoints("John")As far as I can see, you are trying to combine get and set in one function.
You may want to use a
Points constructor functionhere, something like: