// Create a new user object that accepts an object of properties
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 ) { 
    
        
        
        // Create a new getter for the property
        this[ "get" + i ] = function() {
          
        
            return properties[i];
        };        // Create a new setter for the property
        this[ "set" + i ] = function(val) {
          
        
            properties[i] = val;
        };
       
   
      
       
    }
   
    
}// Create a new user object instance and pass in an object of
// properties to seed it with
var user = new User({
    name: "Bob",
    age: 44
    
    });// Just note that the name property does not exist, as it's private
// within the properties object
alert( user.name == null );// However, we're able to access its value using the new getname()
// method, that was dynamically generatedalert( user.getname() == "Bob" );//为什么user.getname() 取的是user.getage()的值???????????? // Finally, we can see that it''s possible to set and get the age using
// the newly generated functions
user.setage( 22 );alert( user.getage() == 22 );

解决方案 »

  1.   

    闭包问题,见我写的注释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 ) { 
        
            
            //闭包外,i的值还没变
            // Create a new getter for the property 
            this[ "get" + i ] = function() { 
            //闭包内,i的值在进闭包的时候已经改变,和外面的i的值不同,这里要用以个变量来记录外面传进来的i值,就不会出错了
            
                return properties[i]; 
            };         // Create a new setter for the property 
            this[ "set" + i ] = function(val) { 
            
            
                properties[i] = val; 
            }; 
          
      
          
          
        } 
      
        
    } // Create a new user object instance and pass in an object of 
    // properties to seed it with 
    var user = new User({ 
        name: "Bob", 
        age: 44 
        
        }); // Just note that the name property does not exist, as it's private 
    // within the properties object 
    alert( user.name == null ); // However, we're able to access its value using the new getname() 
    // method, that was dynamically generated alert( user.getname() == "Bob" );//为什么user.getname() 取的是user.getage()的值???????????? // Finally, we can see that it''s possible to set and get the age using 
    // the newly generated functions 
    user.setage( 22 ); alert( user.getage() == 22 );
      

  2.   

    这样就对了<script language="javascript">
    function User( properties ) {
        //遍历对象属性,确保它作用域正确
        for ( var i in properties ) { (function(which){ var p=i
            //为属性创建获取器
            which[ "get" + i ] = function() {
                return properties[p];
            };
            //为属性创建设置器
            which[ "set" + i ] = function(val) {
                properties[p] = val;
            };
        })(this); }
    }
    var user = new User({
        name: "Bob",
        age: 44
    });
    alert( user.name == null );
    alert( user.getname() == "Bob" );
    user.setage( 22 );
    alert( user.getage() == 22 );
    </script>
      

  3.   

    1楼的分析是对的,不过没给解决方案for ( var i in properties ) {
        (function(i) {
            // Create a new getter for the property 
            this[ "get" + i ] = function() {
                return properties[i]; 
            }; 
            // Create a new setter for the property 
            this[ "set" + i ] = function(val) {
                properties[i] = val; 
            };
        })(i);