Skip to content

Instantly share code, notes, and snippets.

@tangrammer
Created December 23, 2012 20:22
Show Gist options
  • Save tangrammer/4365803 to your computer and use it in GitHub Desktop.
Save tangrammer/4365803 to your computer and use it in GitHub Desktop.
utility to init object with object with data specification and rubylike getter methods in models (functions constructors)
var init=function (o, spec){
for(value in spec){
if(o[value]==undefined)
o[value]=spec[value];
}
};
var getters=function(f, props){
for(var i=0; i<props.length; i++){
var mi_get=function(x){
return function(){
return this[props[x]];
};
}(i);
f.prototype['get_'+props[i]]=mi_get;
}
};
var data_person_example={
id: 1,
fname: "Juan Antonio",
lname: "Ruz",
DOB: "1976-06-13",
wage: 100,
location: "ES"
};
function Person(){
getters(this, ["id", "fname", "lname", "DOB", "wage", "location"]);
};
function create_person(spec){
var p=new Person();
init(p, spec);
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment