2

How to reference an object inside itself?

As you can see below I'm creating an unnamed object inside the console.log

 console.log({ x: 2, y: this.x * 2, }); 

I'm not assigning it to any variable.

So I want a way to access it's property x inside itself like calling this.x.

However the above approach is not working.

I'm getting a NAN error.

NOTE I don't want it to assign to a variable/ create using a function constructor/ prototype.

7
  • Just use 2 If it's a variable, use the variable Commented Oct 8, 2020 at 14:10
  • If I just use the variable then it throws x not defined error... Hope you got my point. Commented Oct 8, 2020 at 14:11
  • 1
    Does this answer your question? Can I reference other properties during object declaration in JavaScript? Commented Oct 8, 2020 at 14:12
  • 2
    well you can't... Commented Oct 8, 2020 at 14:14
  • 3
    There is no mechanism that allows an "under construction" object to be referenced inside the object initializer. You can partially construct the object and then use separate statements to add properties based on the values of other properties, or you can use the getter approach as suggested in the answer below to defer computation to the point where a property value is accessed. Commented Oct 8, 2020 at 14:19

3 Answers 3

5

You could write y with a getter method:

The get syntax binds an object property to a function that will be called when that property is looked up.

console.log({ x: 2, get y() { return this.x * 2 }, });

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

12 Comments

is this javascript?
I mean if I try it from my side .. I'm only getting {x:2} .... nothing else!
Assign it to an object like: var obj = { x: 2, get y() { return this.x * 2 }}; Then call it: obj[y]
JavaScript provides no direct way of doing that.
This is the best solution your going to get for what you want... For the context issue, you could create a function that returns the object as described here.
|
0

You could set your initial property first, and then the property that references it later:

const obj = {'x':2}; obj.y = obj.x*2; console.log(obj);

5 Comments

Without any context about why you need to do it this way, it's hard to give you a good answer.
I wanna do it in one go... I don't wanna assign to a variable..
Inside the object initializer, the object does not yet exist. The language provides no way of referring to the object until after the initializer is evaluated.
My bad... maybe I'm wrong! sorry... it may not be possible... I thought it would be!
If you wanna do it in one go, and you already know what the value of x is going to be as you're initializing it, then make y the value of 2 times that, without referencing x.
0

Just create a function that will take the first object as first arg and a callback in the second, then in the callback you would work on the things you set, return what you want, and merge it back to the original object.

console.log(mergeFunction({ x: 2 }, (o) => { return { y: o.x * 2 }} })); 

Not pretty, but likely the best you can do if you want it in "one go".

You get to implement mergeFunction yourself.

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.