<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题</title>       
    
<script type="text/javascript"> 
function User(properties)
{//遍历该对象的所有属性。保证其作用域正确
for(var i in properties)
{
(function(which){
var p=i;
//创建此属性的一个新的读取器(getter)
which["get"+p]=function(){
return properties[p];
}
// 创建此属性的一个新的设置器。(setter)
which["set"+ p ]=function(val){
properties[p]=val;
};
})(this);
}
}
var user=new User({
name:"Bob",
age:44
});
 //注意 name属性并不存在,因为它是属性的对象 的私有变量
alert(user.name==null);
//可以用getname来获取值。因为此函数是动态生成的
alert(user.getname()=="Bob");
//可以用新生成的函数来设置或获取年龄
user.setage(22);
alert(user.getage()==22);
 
</script>
     
 
</head>
<body>
    
            
             
  
  
</body>
</html>ml>