0

how to object value to its on property object ?

I tried like this

var obj = {
 a:2,
 b:this.a   
}

Then try obj.b it is giving undefined ..can I make function in object?

Expected output 2

user5711656
  • 2,795
  • 3
  • 29
  • 53

1 Answers1

1
const obj = {
  a: 2,
  get b() {
    return this.a;
  }
};

console.log(obj.b);

const obj2 = {
  a: 2,
  b: function() {
    return this.a;
  }
};

console.log(obj2.b());

See it in action: https://jsfiddle.net/rqnbxw86/

Mario Murrent
  • 682
  • 5
  • 22
anttud
  • 711
  • 3
  • 9