Skip to content

Instantly share code, notes, and snippets.

@godspeedyoo
Created February 11, 2015 04:26
Show Gist options
  • Save godspeedyoo/73efa620b3ecc85da924 to your computer and use it in GitHub Desktop.
Save godspeedyoo/73efa620b3ecc85da924 to your computer and use it in GitHub Desktop.
Javascript Practice - Dynamic Getter and Setter Functions
function Person(properties) {
for (var i in properties) {
var getProperty = i[0].toUpperCase() + i.slice(1);
this["get" + getProperty] = function() {
return properties[i];
}
};
for (var i in properties) {
var setProperty = i[0].toUpperCase() + i.slice(1);
this["set" + setProperty] = function(data) {
properties[i] = data;
}
}
}
bob = new Person({name: "Bob", age: "20", hatSize: 1})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment