[ edit ] предыдущий ответ был неправильным: не упомянул невозможность getPoints("John")
Насколько я понимаю, вы пытаетесь объединить get и set в одинфункция.
Вы можете использовать Points constructor function
здесь, что-то вроде:
var Points = function(name,points){
this.name = name || '';
this.points = points || 0;
if (!Points.prototype.get){
var proto = Points.prototype;
proto.get = function(label) {
return this[label] || label
};
proto.set = function(){
if (arguments.length === 2){
this[arguments[0]] = arguments[1];
} else if (/obj/i.test(typeof arguments[0])){
var obj = arguments[0];
for (var l in obj){
if (obj.hasOwnProperty(l)){
this[l] = obj[l];
}
}
}
return this;
};
}
}
var john = new Points('John',0), mary = new Points('Mary',2), pete = new Points;
pete.set({name:'Pete',points:12});
john.set('points',15);
//two ways to get a 'points' property
alert(john.get('points')+', '+pete.points); //=> 15, 12