Crappy prototype test
by moob
JavaScript
function Person(args) {
if(args === undefined){throw new Error('Missing required arguments!');}
this._name = args.name || 'anon';
this._age = args.age || null;
}
Person.prototype = {
test : function(){return this._name || undefined;},
name : function(val){
//this format sets the value or retrieves it
if(val){this._name=val;} else {return this._name || undefined;}
},
set : function(key,val){
//this only sets
key = "_"+key;
if(this[key]){
this[key] = val;
}
}
}
console.log('x');
var Geoff = new Person({name:'Geoff', age:20});
alert(Geoff.name());
Geoff.name("bob");
alert(Geoff.name());
Geoff.set("name","gerry");
alert(Geoff.name())
//var Scott = new Person();//throws error
//alert(Geoff.test()+" "+Scott.test());