简体   繁体   English

如何链接两个JavaScript对象?

[英]How can I link two JavaScript objects?

Say I have an object (obj1) where one of the values in obj1 is another object (obj2). 假设我有一个对象(obj1),其中obj1中的一个值是另一个对象(obj2)。 Is there any way to make it so that checking for anything in obj1 (eg obj1.username) would also check in obj2 (obj1.ob2.username) automatically? 有没有办法让它在obj1中检查任何东西(例如obj1.username)也会自动检入obj2(obj1.ob2.username)?

The use case for this is if something doesn't exist in obj1, I would want to also check in obj2 before it returns null. 用例是如果obj1中不存在某些内容,我还想在返回null之前检​​查obj2。

Example Usage: 用法示例:

 var user = {username: "David"} // obj2 var member = {nickname: "Dave", user: user} // obj1 console.log(user.username) // "David" console.log(user.nickname) // null console.log(member.username) // "David" console.log(member.nickname) // "Dave" 

This is for usage in Node.js. 这适用于Node.js.

--Edit-- - 编辑 -

I forgot to note that members and users are created from classes in a library I'm using to interact with an application called Discord. 我忘了注意,成员和用户是从我用来与名为Discord的应用程序进行交互的库中的类创建的。

When a message is sent an event is triggered and I am handed information like the member object (with a link to the user object inside it). 当发送消息时,触发事件并且我将信息传递给成员对象(其中包含用户对象的链接)。

Here is the structure of a user. 这是用户的结构。

Here is the structure of a member. 这是一个成员的结构。

Object.defineProperty(Discord.Member.prototype, 'username', {
    get: function() { return this.user.username; },
    set: function(newValue) { this.user.username = newValue; }
});

The above method works as intended, however only for the username property. 上述方法按预期工作,但仅适用于username属性。 I considered applying this for every property of user, however when I tried iterating through the properties of a created user, some (like .createdAt ) didn't show up in the list. 我考虑过将这个应用于用户的每个属性,但是当我尝试迭代创建用户的属性时,一些(如.createdAt )没有显示在列表中。

I understand that I could do it manually and write every defineProperty by hand, but the idea here is that user will be constantly updated with new properties over time, so I would prefer it to do what I described above. 我知道我可以手动完成并手动编写每个defineProperty,但这里的想法是用户将随着时间的推移不断更新新属性,所以我更喜欢它做我上面描述的。

You could use Proxy and inside use getter to check first if property exists in original and then in other object. 您可以使用Proxy和内部使用getter来检查属性是否存在于原始属性中,然后用于其他对象。

 var user = { username: "David" } // obj2 var member = { nickname: "Dave", user: user, } // obj1 var pMember = new Proxy(member, { get: function(target, prop) { if (target[prop]) return target[prop]; else if (target.user[prop]) return target.user[prop]; } }); console.log(pMember.nickname) console.log(pMember.username) 

Using inheritance: 使用继承:

var user = {username: "David"} // obj2
var member =Object.assign(Object.create(user),{nickname: "Dave", user: user}); // obj1

console.log(member.username);

http://jsbin.com/nojolizewu/edit?console http://jsbin.com/nojolizewu/edit?console

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM