Created
February 11, 2015 04:26
-
-
Save godspeedyoo/73efa620b3ecc85da924 to your computer and use it in GitHub Desktop.
Javascript Practice - Dynamic Getter and Setter Functions
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
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