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'.

5
  • 1
    Only my_object.value3() Commented Oct 28, 2018 at 14:56
  • 1
    Also fix the reference to value3_2 — should be this.value3_2 Commented Oct 28, 2018 at 14:57
  • Ahh! Of course. It's a function (Method) and thus I need the parenthesis. Thanks. Please feel free to post it as an answer instead of a comment. Commented Oct 28, 2018 at 14:58
  • 1
    Possible duplicate of Call functions from function inside an object (object literal) Commented Oct 28, 2018 at 14:58
  • Use getter. See my answer below. Commented Oct 28, 2018 at 15:15

3 Answers 3

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]))
Sign up to request clarification or add additional context in comments.

7 Comments

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.
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.
Updated my answer with examples. newbie or not, you deserve the answers.
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.
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.
|
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.

1 Comment

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.
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;
 }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.