| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 919 人关注过本帖
标题:求助,急~~ 谢谢!
只看楼主 加入收藏
国际在线
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-8-27
结帖率:100%
收藏
 问题点数:0 回复次数:2 
求助,急~~ 谢谢!
<html>
<script>
  // 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 propperly scoped (as discussed previously)
   for (var i in properties){
    (function(){
     // 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;
      };
    })();}
   
  }
  alert(1);
  // 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 ther 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");
  
  // 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);
</script>
</html>


这是一段测试privilleged 方法的javascript 代码,运行到alert(user.getname()=="Bob");的时候就出错了。说是对象不支持此方法。
搜索更多相关主题的帖子: previously function property through 
2008-08-27 13:01
渚薰
Rank: 6Rank: 6
等 级:贵宾
威 望:22
帖 子:1132
专家分:0
注 册:2006-8-6
收藏
得分:0 
闭包函数调用的里的this,不在是User的对象

function User(properties){
   // Iterate through the properties of the object, and make sure
   // that it's propperly scoped (as discussed previously)
   var _this = this; //pass the 'this' for User Object into the closure.
   for (var i in properties){
    (function(){
     // 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;
      };
    })();}
}

另外这样对属性赋值是不正确的,因为存在闭包,所以你的你的getname,getage永远是对age进行取值,赋值


简单的例子:
var c = function() {
    var obj = {
        a:1,
        b:2,
        c:3
    }
   
    for (var p in obj) {
        this['get' + p] = function() {
              return obj[p];
        }
    }
}

var cc = new c();
c.geta();  
>>>>>3
c.getb();
>>>>>3
c.getc();
>>>>>3


因为
function() {
        return obj[p];
}
对于变量p来说是个闭包
方法被执行时才去检测p的值,而经过for循环后,p的值已经是c
所以 这个方法永远返回obj.c

不懂闭包不要乱用
多去看看闭包的知识

另外关于js的模仿类的访问权限,先看看这篇文章:
http://www.

[[it] 本帖最后由 渚薰 于 2008-8-27 14:11 编辑 [/it]]

个人ajax技术专题站: " target="_blank">http://www. 我不会闲你烦,只会闲你不够烦!
2008-08-27 14:08
国际在线
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-8-27
收藏
得分:0 
嗯,好的,谢谢渚薰GG的热心帮忙。
2008-08-27 18:22
快速回复:求助,急~~ 谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045011 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved