Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Object value calls a function, but all the values for the function trigger when importing object

I have an issue where I’m importing an object into another file. Object looks like:

export const MyObject = { 
      value1: mergeObject({object of data}),
      value2: mergeObject({object of data}),
      ...
};

The object values call a function where I’m merging two objects. The issue is that when I’m importing MyObject to another file and I’m attempting to access a single value all of the functions for each value fire off.

I anticipated just being able to import the object like normal and access the values like MyObject.value1, but the function was being called for every value of the object.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because that’s exactly what this syntax does:

{
  value1: mergeObject({object of data}),
  value2: mergeObject({object of data})
}

To build this object literal, it is evaluating the expressions for its properties. It sounds like you want those properties to be functions, not values. Something like this:

{
  value1: () => mergeObject({object of data}),
  value2: () => mergeObject({object of data})
}

And then you’d invoke the function when you want to use it:

MyObject.value1()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading