【问题标题】:Object.create() not fully creating;Object.create() 没有完全创建;
【发布时间】:2012-06-19 06:35:21
【问题描述】:

代码如下:

//Note: x actually isn't defined, I'm pulling it from an external source

var x = [{ name: 'Michael Lovesllamas Lankford',
 created: 1338420951.11,
 laptop: 'pc',
 laptop_version: null,
 userid: '4fc6aed7eb35c14ad6000057',
 acl: 0,
 fans: 1,
 points: 5,
 avatarid: 34 }]

global.UserBase = {
 userid: -1,
 name: "noidea",
 isSuperUser: false,
 isDJ: false,
 laptop: "pc",
 afkWarned: false,
 afkTime: Date.now(),
 droppedTime: null,
 droppedRoom: null,
 songCount: 0,
 mWaitingSongLimit: 0,
 totalSongCount: 0,
 totalHeartCount: 0,
 totalHeartsGiven: 0,
 customGreeting: null,
 bootAfterSong: false,
 joinedTime: Date.now(),
 whiteList: false,
 allowedToReserveSpot: true
};

global.mUsers = {length:0};

global.Register = function(a) {
 for(var i = 0; i < a.length; i++) {
  var sUser = a[i];
  mUsers[sUser.userid] = CreateUser(sUser);
  mUsers.length++;
 }
};

global.CreateUser = function(a) {
 var b = Object.create(UserBase);
 b.userid = a.userid;
 b.name = a.name;
 b.laptop = a.laptop;
 if (a.acl > 0) b.isSuperUser = true;
 return b;
};

Register(x);

现在,解决问题。而不是 mUsers[sUser.userid] 变成这样:

'4fc6aed7eb35c14ad6000057': {
 userid: "4fc6aed7eb35c14ad6000057",
 name: "noidea",
 isSuperUser: false,
 isDJ: false,
 laptop: "pc",
 afkWarned: false,
 afkTime: Date.now(),
 droppedTime: null,
 droppedRoom: null,
 songCount: 0,
 mWaitingSongLimit: 0,
 totalSongCount: 0,
 totalHeartCount: 0,
 totalHeartsGiven: 0,
 customGreeting: null,
 bootAfterSong: false,
 joinedTime: Date.now(),
 whiteList: false,
 allowedToReserveSpot: true
}

变成这样:

'4fc6aed7eb35c14ad6000057': { 
 userid: '4fc6aed7eb35c14ad6000057',
 name: 'Michael Lovesllamas Lankford',
 laptop: 'pc' 
}

任何想法为什么 UserBase 中的其余值没有被添加到对象中?

【问题讨论】:

    标签: javascript node.js object


    【解决方案1】:

    Object.create 创建一个新对象并将其prototype 设置为您传入的对象。

    您的对象正在初始化,但您只是在新对象实例上显式设置了一些属性。原型对象 (UserBase) 的属性都可以访问,但对象的直接console.log 不会打印它们。

    例如运行代码后,执行:

    for(var p in mUsers['4fc6aed7eb35c14ad6000057']) {
      console.log(p, mUsers['4fc6aed7eb35c14ad6000057'][p]);
    }
    

    打印出来:

    userid 4fc6aed7eb35c14ad6000057
    name Michael Lovesllamas Lankford
    laptop pc
    isSuperUser false
    isDJ false
    afkWarned false
    afkTime 1340089066700
    droppedTime null
    droppedRoom null
    songCount 0
    mWaitingSongLimit 0
    totalSongCount 0
    totalHeartCount 0
    totalHeartsGiven 0
    customGreeting null
    bootAfterSong false
    joinedTime 1340089066700
    whiteList false
    allowedToReserveSpot true
    

    【讨论】:

    • 好的,我刚刚测试过,它有效。傻我。但是,我仍然可以通过 mUsers[var].songCount 访问它吗?我想知道如果我将它保存到数据库中它将如何工作。 (我正在使用 couchdb 来存储这些对象)
    • 是的,您可以直接访问属性。尝试在对象上调用 JSON.stringify - 这很可能是您用来访问 couchdb 的任何内容。
    • 我决定从头开始创建函数 CREATE 一个对象,而不是使用模板,所以 b = {}; b.userid = 用户ID;等等。不过感谢您的帮助!
    【解决方案2】:

    UserBaseglobal 对象上,您需要在global 对象上调用Register

    global.CreateUser = function(a) {
     var b = Object.create(this.UserBase);
     b.userid = a.userid;
     b.name = a.name;
     b.laptop = a.laptop;
     if (a.acl > 0) b.isSuperUser = true;
     return b;
    };
    
    global.Register(x);
    

    【讨论】:

      【解决方案3】:

      发生这种情况是因为您只是在初始化 用户身份, 名称和笔记本电脑。 查看您的代码:

      b.userid = a.userid;
      b.name = a.name;  
      b.laptop = a.laptop; 
      

      要得到想要的结果,你需要调用 Global.Register(x) 而不是 Register(x)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-12-19
        • 1970-01-01
        • 2022-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        相关资源
        最近更新 更多