0

Say I have the following object:

var my_object = {
    value1: "220",
    value2: "100",
    value3_1: "300",
    value3_2: "300",
    value3: function() {
        return +this.value3_1 + +value3_2;
    }
};

What do I need to do to make this work? In console, my_object.value3 returns:

() {
        return +this.value3_1 + +this.value3_2;
    }

..instead of '600'.

3 Answers3

2

Use getter

var my_object = {
 value1: 220,
 value2: 100,
 value3_1: 300,
 value3_2: 300,
 get value3() {
    return this.value3_1 + this.value3_2;
 }
};

console.log(my_object.value3);

This way you don't need to "know" which one is a function and which is not.

For example, say you need to read all values, you can do:

Object.keys(my_object).forEach(k => console.log(k, my_object[k]))

Whereas without getter, you'll need to:

Object.keys(my_object).forEach(k => console.log(k, 
    typeof my_object[k] === 'function' ? my_object[k]() : my_object[k]))
niry
  • 3,238
  • 22
  • 34
  • Perhaps knowing is a good thing? Especially for a newbie like me that is still struggling with syntax. It's there another raccoon for using get? What does is mean? Can't just use it if I don't understand it. – Streching my competence Oct 28 '18 at 15:15
  • To me it looks like you are suing that you want to get a prop that doesn't exist (reversed word logic) and it makes me uncomfortable in that I'll remember it. – Streching my competence Oct 28 '18 at 15:18
  • 1
    Updated my answer with examples. newbie or not, you deserve the answers. – niry Oct 28 '18 at 15:30
  • Yeah sorry, that just introduced more questions like "what is key?", "is it a new object, even though my_object is an object literal and not a constructor?", "why write object first?", "Why is forEach appended to key?", "what is 'k'?". Sure, less code, so that's good. Perhaps I can revisit this and utilize it in a few months. – Streching my competence Oct 28 '18 at 15:36
  • Objects (e.g my_object) are made of key:value pairs. Object.keys() is a function that will return an array of all the keys in a given object. .forEach is an array function that will callback the given function, for each element in the array. – niry Oct 28 '18 at 15:40
  • Oh, that was easier to understand than expected. And "k" is just some variable and could be whatever instead of "k" specifically? – Streching my competence Oct 28 '18 at 15:50
  • k (short for key) is just the argument of the arrow function (=>). you could use regular function too: `function(k) {console.log(k, my_object[k]); }` – niry Oct 29 '18 at 01:34
0

You possibly want to do this :

var my_object = {
 value1: "220",
 value2: "100",
 value3_1: 300,
 value3_2: 300,
 value3: function() {
    return +this.value3_1 + this.value3_2;
 }
};

and then call the function with this : my_object.value3()

Note: I added this in front of value3_2 and I changed the value of value3 _1 and value3_2 to numbers from string be removing the double quotes.

ishanbakshi
  • 1,915
  • 3
  • 19
  • 37
  • Perfect! I thought it was weird to have to write the plus in front of the strings, and since I'm using numbers for calculations then this makes more sense of course. – Streching my competence Oct 28 '18 at 15:13
0

When you are calling a function, you need to use parenthesis (). So it should be my_object.value3().

Also you have to replace value3_2 with this.value3_2

Alternatively, value3_1 and value3_2 are both numbers, so no need to give quotes. Simply you can write 300.

var my_object = {
  value1: 220,
  value2: 100,
  value3_1: 300,
  value3_2: 300,
  value3: function() {
    return this.value3_1 + this.value3_2;
 }
};
Lakshmi
  • 46
  • 4