-1

I have following object. And I want to call object method without parentheses.

let obj = {
    'a': 1,
    'm': 1,
    'b': function () {
        let pc = 2013;
        if (pc >= 1000) return 3.4;
    }
};

result = obj['a'];    //1
result = obj['b'];    //f
result = obj['b']();  //3.4

How do I do that?

kyore
  • 812
  • 5
  • 24

1 Answers1

1

You can use accessor methods like this:

let obj = {
    'a': 1,
    'm': 1,
     get b () {
        let pc = 2013;
        if (pc >= 1000) return 3.4;
    }
};

console.log(obj.b)
Bogdan Veliscu
  • 641
  • 6
  • 11