function User(properties)
{
    for (var i in properties)
    {
        (function(which)
        {
            var p=i;
            which["get" + p] = function(){return properties[p];};
            which["set" + p] = function(val){properties[p] = val;};
         })(this);
     }
}
问题1:properties应该是数组吗?
问题2:(....)(this);这句是什么意思呢?
问题3:i没有初始值,他是怎么运行的呢?
问题4:which["get" + p] = function(){return properties[p];};这种赋值方法的意思是什么?是让which["get"+p]的值是properties[p]吗?谢谢!

解决方案 »

  1.   


    1. properties是对象
    2. (....)(this); 是定义了一个内部函数,并立即执行, 实参是this,也就是新建立的User对象,(我猜测User应该是一个构造函数)。
    3. JavaScript的内部函数跟它的父函数共享局部变量的作用域,所以 i 就是for循环里面的循环变量i
    4. 假设properties对象有个属性name, 那么这一句
          which["get" + p] = function(){return properties[p];};
       的作用应该是给新建的User对象定义了一个 getName方法,也就相当于
          which.getName = function() { return properties.name;};这段代码多处用到Closure,太让人头疼了,以上仅供参考,楼下继续...
      

  2.   

    1、是集合
    2、this是将自身的实例传进去,这是闭包的知识
    3、i不是没初始值, 而是相当于foreach, i是每次循环获得集合中的对象。
    4、没其他代码 我也看不懂。4楼错得凶!!
      

  3.   

    这是一个JS的模拟类,作用是读、写操作JSON对象properties,其中get是读,set是改写,运行下面代码就明白了。
    trying:<script type=text/javascript>
    function User(properties) 

        for (var i in properties) 
        { 
            (function(which) 
            { 
                var p=i; 
                which["get" + p] = function(){return properties[p]}; 
                which["set" + p] = function(val){properties[p]=val}; 
            })(this); 
        } 
    } var obj = new User({name:"张三",sex:"男",age:"18岁",moh:function(x){return x*x}});with(obj)
    alert(getname() + ":" + getsex() + "," + getage()+ ","+ getmoh()(3)),//读属性setname("李四"), setsex("女"), setage("17岁"),setmoh(function(x){return Math.sqrt(x)}),//写属性alert(getname() + ":" + getsex() + "," + getage()+ ","+ getmoh()(3));
    </script>
      

  4.   

    set get 存取器??? 4楼也没错啊
      

  5.   

    问题1:properties应该是数组吗? 可以Javascript的任何对象(Object/Function)
    问题2:(....)(this);这句是什么意思呢? function(which)的作用域在this里面,本身是一个私有匿名函数。看后面例子.
    问题3:i没有初始值,他是怎么运行的呢?就是 forEach,运行时对于每一个property, i=property
    问题4:which["get" + p] = function(){return properties[p];};这种赋值方法的意思是什么?是让which["get"+p]的值是properties[p]吗? 也就是说,调用get*方法时调用后面的函数.其实作为User构造的Object 参数{name:"张三",sex:"男",age:"18岁",moh:function(x){return x*x}}本身是个对象字面量,可以直接以点的形式访问和赋值 var a={name:"张三",sex:"男",age:"18岁",moh:function(x){return x*x}}, a.name="李四".这里我把this该成window. 其实是关于作用域和闭包的知识.function User(properties) 

        for (var i in properties) 
        { 
            (function(which) 
            { 
                var p=i; 
                which["get" + p] = function(){return properties[p]}; 
                which["set" + p] = function(val){properties[p]=val}; 
            })(window); 
        } 
    } var obj = new User({name:"张三",sex:"男",age:"18岁",moh:function(x){return x*x}});with(obj)
    alert(getname() + ":" + getsex() + "," + getage()+ ","+ getmoh()(3)),//读属性setname("李四"), setsex("女"), setage("17岁"),setmoh(function(x){return Math.sqrt(x)}),//写属性alert(getname() + ":" + getsex() + "," + getage()+ ","+ getmoh()(3));var obj1 = new User({name:"张三",sex:"男",age:"18岁",moh:function(x){return x*x}});
    with(obj)   //注意看这里
    alert(getname() + ":" + getsex() + "," + getage()+ ","+ getmoh()(3))
      

  6.   


    这段代码的作用就是在User中定义properties的属性的getter和setter
    很精简,写出这个代码的js应该很牛了