function User( properties ) { // Iterate through the properties of the object, and make sure // that it's properly scoped (as discussed previously) for ( var i in properties ) { (function(thiz){ // Create a new getter for the property thiz[ "get" + i ] = function() { return properties[i]; }; // Create a new setter for the property thiz[ "set" + i ] = function(val) { properties[i] = val; }; })(this); } }
this 必须传入闭包内。