Object.create() 未完全创建;.
Object.create() not fully creating;
这是代码:
//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'
}
知道为什么用户库中的其余值没有添加到对象中吗?
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
UserBase
global
对象上,您需要在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);
发生这种情况
是因为您只是在初始化用户标识,名称和笔记本电脑。查看您的代码:
b.userid = a.userid;
b.name = a.name;
b.laptop = a.laptop;
要获得所需的结果,您需要调用 Global.Register(x) 而不是 Register(x)
相关文章:
- 在使用object.create创建的对象中使用super
- 正在使用object.create()创建XMLHttpRequest对象
- Apache poi XSSF创建Excel文件-Create返回格式或文件扩展名无效的空文件
- 正在对通过object.create创建的对象设置原型
- 在不使用object.Create的情况下创建具有null原型的javascript对象
- 使用Object.create()创建对象的模式
- Ember模型create()没有创建ID,适配器必须实现“;createRecord”;
- 使用“new”关键字创建的对象和使用“Object.create”创建的对象给出不同的结果
- 如何使用 Object.create() 而不是 new 创建具有私有成员的对象
- Object.create() 未完全创建;.
- Chrome扩展程序:将数据发送到使用chrome.windows.create创建的窗口
- 设置 使用 Object.create() 创建的 JavaScript 对象的属性
- 如何判断对象是使用“Object.create”还是使用文字语法/“new”关键字创建的
- 如何使用object.create创建对象
- 是否可以使用 Object.create 模式来创建 CustomEvent 对象
- 使用 Object.create() 和 Object.assign() 创建对象有什么区别?
- 对于使用Object.create()创建的数组,Array.isArray()返回false
- JavaScript:函数是否应该能够使用Object.create()创建自己的实例
- 使用javascript中的object.create(someprototype)创建的对象的构造函数
- 使用Object.create创建的Javascript数组-不是真正的数组