Created
December 23, 2012 20:22
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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