-1

I have a set of objects, that contain references to other objects. There is always a general reference: nextObject. But sometimes, I know more about the object beside me. So I could use a member like: theNextSentenceInThisParagraph.

Some code, that knows what they are dealing with, could use tNSITP. Other code, that doesn't know about the adjacent object, would use nO. I could assign two members, each with its own name, pointing to the adjacent object. But I'd rather assign two names to the same member. Is that possible?

myObject
    nextObject: theNextSentenceInThisParagraph: reference-to-next-object
3
  • 7
    This is not javascript and your code looks like yaml. Please, update your question Commented Jan 15 at 15:18
  • This question should not have been closed. It asks a clear question. You can tell, because it received an answer. That answered the question, and was accepted! But oh well . . . Commented Jan 16 at 16:49
  • It was closed because the code block is pseudocode that looks like YAML but the question is tagged "javascript". I answered in good faith because your question text only talks about javascript, but I can see why the people who voted to close would want the question to be clarified. Commented Jan 17 at 22:04

1 Answer 1

2

Sure, but not with the syntax you're proposing. What you're looking for is get and/or set:

const obj = {
  foo: 0,
  get bar() {
    return this.foo;
  },
  set bar(value) {
    this.foo = value;
  },
};

console.log('foo', obj.foo);
console.log('bar', obj.bar);

obj.foo = 1;

console.log('foo', obj.foo);
console.log('bar', obj.bar);

obj.bar = 2;

console.log('foo', obj.foo);
console.log('bar', obj.bar);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.