Sunday, 30 April 2017

How can I link two JavaScript objects?

Say I have an object (obj1) where one of the values in obj1 is another object (obj2). Is there any way to make it so that checking for anything in obj1 (e.g. obj1.username) would also check in obj2 (obj1.ob2.username) automatically?

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.

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.



via Vaeb

No comments:

Post a Comment