var aPerson = myObjWrap("Name, Age, Gender");
from there, my code creates the 'aPerson' object so you can perform functions on it like so:
aPerson.setName("Brendon");
aPerson.setAge("24");
aPerson.setGender("Male");
And then if you want to see what the object contains you can do something like this:
console.log("Name: " + aPerson.getName() + " Age:" + aPerson.getAge() + " Gender: " + aPerson.getGender() );
Which would spit out: Name: Brendon Age:24 Gender: Male
So here's the code
//code for trim
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g,'');
};
}
$(document).ready(function() {
function AddGettersAndSetters(object) {
for (var field in object) {
if (typeof(object[field]) != 'function') {
if (typeof(object[field]) == 'object') {
for (var f = 0; f < object[field][0].length; f++) {
AssignFunctions(object, object[field][0][f].trim() );
}
} else {
AssignFunctions(object, field);
}
}
}
function AssignFunctions(object, field) {
object['get' + field] = function() {
return this[field];
};
object['set' + field] = function(value) {
this[field] = value;
};
}
}
var myObjWrap = function(fields) {
var functions = new Array(fields.split(","));
var theObj = {
fields: functions
};
var gs = new AddGettersAndSetters(theObj);
return theObj;
}
var aPerson = myObjWrap("Name, Age, Gender");
console.log( aPerson );
aPerson.setName("Brendon");
aPerson.setAge("24");
aPerson.setGender("Male");
console.log( aPerson );
console.log("Name: " + aPerson.getName() + " Age:" + aPerson.getAge()
+ " Gender: " + aPerson.getGender() ); });
No comments:
Post a Comment