depravedAngel1833 2009-03-18 14:04
浏览 160
已采纳

pro JavaScript Techniques书中使用闭包实现属性getter、setter方法

书中,在User的函数的for循环中,通过闭包实现getter、setter方法,但我试了一下,程序运行时报错,
user.getname() 方法不存在。如果将闭包去除,user.getname()与user.getage()方法取得的值都是44,显然不对了,可就是不知道问题出在哪边。感觉这边通过使用闭包应该是可以实现getter、setter方法,但不知道为什么使用闭包后,总是提示getname等不是一个function。代码如下:
[code="java"]


<br> // the exaple in pro JavaScript Techniques</p> <pre><code> // 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&#39;s properly scoped (as discussed previously) for ( var i in properties ) { //(function(){ // Create a new getter for the property this[ &quot;get&quot; + i ] = function() { return properties[i]; }; // Create a new setter for the property this[ &quot;set&quot; + 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: &quot;Bob&quot;, age: 44 }); // Just note that the name property does not exist, as it&#39;s private // within the properties object alert( user.name == null ); // However, we&#39;re able to access its value using the new getname() // method, that was dynamically generated alert( &quot;name:&quot; + user.getname() + &quot;,age:&quot; + user.getage()); // Finally, we can see that it&#39;&#39;s possible to set and get the age using // the newly generated functions user.setage( 22 ); alert( user.getage() == 22 ); &lt;/script&gt; </code></pre> <p></head><br> <body><br> </body><br> </html><br> [/code]<br> 望解决,谢谢!</p>
  • 写回答

3条回答 默认 最新

  • xenocide 2009-03-18 14:57
    关注

    原因是第二轮循环里,i变成了'age',所以properties[i]总是相当于properties['age']

    正确的做法如下:
    [code="javascript"]
    for ( var j in properties ) {
    var obj = this; //保存this,因为下面那个匿名function里面的this不同了
    (function(i){
    //如果用this,定义的getter和setter就不是user的了
    obj[ "get" + i ] = function() {
    return properties[i];
    };

    obj[ "set" + i ] = function(val) {
    properties[i] = val;
    };
    })(j); //将j传入匿名函数中
    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料