You would need to add a closure within the for loop that holds the current value of each loop, hope this helps. Example: http://jsfiddle.net/subhaze/aDb6Q/
<http://jsfiddle.net/subhaze/aDb6Q/>var names = ["test1", "test2", "test3", "test4"] var initValues = { "test1": "test1Val", "test2": "test2Val", "test3": "test3Val", "test4": "test4Val", } var commonGetterFunction = function(name) { return initValues[name] } var commonSetterFunction = function(name, val) { initValues[name] = val; } for (i = 0; i < names.length; i++) { (function() { var propName = names[i]; Object.defineProperty(this, propName, { get: function() { console.log("Inside get: " + propName); return commonGetterFunction(propName); }, set: function(val) { commonSetterFunction(propName, val); } }); console.log(this[propName]); })(); } console.log('-----tests-----'); console.log(this.test1); console.log(this.test2); console.log(this.test3); console.log(this.test4); -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
