1

I'm looking to do something like the task here Self-references in object literals / initializers , except that it would be for the value of an aunt/uncle key, or the sibling key of the parent object. For example:

const obj = {
  parent: {
    child: {
      aunt: /* aunt object */
    }
  },
  aunt: {
    foo: {
      bar: 1
    }
  }
}

There's a very similar ask here Reference nested 'sibling'-property in object literal but unfortunately, not quite what I'm looking for. Ideally, the solution would be extensible and probably would need to be to handle cases where I'd want to access a great-grandcousin object related to a key if need be. Thanks!

reactor
  • 1,722
  • 1
  • 14
  • 34
  • Yes, this is exactly the same as the self-reference in an object literal. You cannot access other values that are not yet created, regardless whether it's on the same level or a sibling of your parent. The solutions are pretty much the same as in the question you found. – Bergi Feb 23 '19 at 21:04
  • The most voted and confirmed answer shows that it is possible with the use of a getter, but the use case is more restricted than mine, which makes me confused as to how one can arrive at the insight that you shared from that answer. – reactor Feb 23 '19 at 21:09
  • 1
    You could use a getter here as well: `{aunt: …, get parent() { return { child: this.aunt }; }}`. Or `const obj = {aunt: …, parent: { get child() { return obj.aunt; }}};`. Although a getter really isn't a good solution in general, even if it's the accepted answer that doesn't invalidate the solutions given in the other answers. Upvote for those that work for you, downvote those that don't. – Bergi Feb 23 '19 at 21:12
  • awesome! thank you! – reactor Feb 23 '19 at 21:17
  • You can accept one answer (if it helps you) by click on big gray check button on its left side. If you wish you can add +10 points to any author of any good answer by click upper gray triangle – Kamil Kiełczewski Feb 24 '19 at 07:30
  • I don't see a big gray check button nor a triangle to the left of Bergi's comment which is the answer that I would accept. – reactor Feb 25 '19 at 02:52

1 Answers1

1

It's not possible in a single object literal. You'd have to define the object first, then assign to the aunt key .

const obj = {
  parent: {
    child: {
    }
  },
  aunt: {
    foo: {
      bar: 1
    }
  }
};
obj.parent.child.aunt = obj.aunt;
console.log(obj.parent.child.aunt === obj.aunt)

Or, you can define aunt beforehand:

const aunt = {
  foo: {
    bar: 1
  }
};
const obj = {
  parent: {
    child: {
      aunt
    }
  },
  aunt
};
console.log(obj.parent.child.aunt === obj.aunt)
Snow
  • 3,820
  • 3
  • 13
  • 39
  • Thanks for the response! I was ideally looking to have an object with nodes at the outermostlayer and then I'd make references to these as needed. I would like to encapsulate all of these nodes in a single object for portability. I was wondering if given these additional constraints, which i think might make it easier(?), what would be the best advised course of action. – reactor Feb 23 '19 at 21:05
  • Lots of methods are possible in programming. If all subobjects will exist on the top level, but the nested paths are only made as needed, then it makes sense to initially define the top level subobjects only. – Snow Feb 23 '19 at 21:27