Suppose I got the following return from a function:
return {
x: 199,
y: 200
}
Note: I can't change the return itself.
How can I then from where I receive the return modify it? That is, for example:
I got x:199
e y:200
and I want to add 20. How can I do it?
What you're returning is an Object , so you can access the object's properties
x
andy
alter them however you want.In your question you refer to
Even if you ask something slightly different later in the text, for completeness it is appropriate to talk about destructuring . An option introduced in ES6 to easily "unpack" objects. In the example that you put, if what we were interested in was really "decomposing" the object returned by the return, you could do something like this:
It's best to create a function that adds functionality.
Imagine a function as follows
You want to add a quick mechanism to add a certain number to both values, so you create another function that abstracts the process, first copies the output of the Original function and defines a more comfortable prototype
Take a look at the full example:
to access an attribute of a json object you must use the
.
, example