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.

mx_code
  • 2,441
  • 1
  • 12
  • 37
  • Just use `2` If it's a variable, use the variable – Guillaume Munsch Oct 08 '20 at 14:10
  • If I just use the variable then it throws x not defined error... Hope you got my point. – mx_code Oct 08 '20 at 14:11
  • I just want to access a property/variable of an object within itself... something like we do with a class ... like accessing own props using this keyword.. – mx_code Oct 08 '20 at 14:12
  • 1
    Does this answer your question? [Can I reference other properties during object declaration in JavaScript?](https://stackoverflow.com/questions/4618541/can-i-reference-other-properties-during-object-declaration-in-javascript) – John Oct 08 '20 at 14:12
  • I want to achieve that with just a raw object.. (i.e. without any contructor/ prototype.) – mx_code Oct 08 '20 at 14:13
  • 2
    well you can't... – Will Jenkins Oct 08 '20 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. – Pointy Oct 08 '20 at 14:19

3 Answers3

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 },
});
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
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);
TKoL
  • 13,158
  • 3
  • 39
  • 73
  • 1
    Without any context about *why* you need to do it this way, it's hard to give you a good answer. – TKoL Oct 08 '20 at 14:23
  • I wanna do it in one go... I don't wanna assign to a variable.. – mx_code Oct 08 '20 at 14:26
  • 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. – Pointy Oct 08 '20 at 14:27
  • My bad... maybe I'm wrong! sorry... it may not be possible... I thought it would be! – mx_code Oct 08 '20 at 14:27
  • 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. – TKoL Oct 08 '20 at 14:33
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.

mjs
  • 21,431
  • 31
  • 118
  • 200