Week3 homework#14
Conversation
…e. Trying to finish it is making me hate learning this at all, so I'm just going to give up on finishing.
| we need to console.log(f1(x)). In the y-code, y is an object. The properties of an object can be altered this way, | ||
| so the function changes the value of x to 10. No alteration has been done to the variable reference itself. If f2 | ||
| attempted to change the *value* of y (rather than an attribute of its object), it would yield results like the x-code. | ||
| For example, if the function is altered, val = { x: 10 }; return val;, it will return { x: 9 } */ |
There was a problem hiding this comment.
I think you get it (?). Another example:
let a = 4
let b = a
a += 1
What's the value of b?
vs objects...
let a = { name: 'gabe' }
b = a
a.country = 'us'
what's the value of b?
Do you see the difference?
There was a problem hiding this comment.
Yep - I assume it has to do with how JS stores them in memory - when b is set to an object, it's a reference to that object, not a new object with the same details. So whenever a property of 'a' changes, 'b' will 'change' as well (because they're both just pointing to the same object). But when a = 4 and b = a, it's setting b equal to a value of 4 (not pointing to an object). So changing the value of 'a' after that doesn't change the value of 'b'.
There was a problem hiding this comment.
So in the objects example, both a and b would have a value of
{
name: 'gabe',
country: 'us'
}
|
Don't merge this to FooCoding repo though ;) |
Removed console.log statement Co-Authored-By: Gabe Rodriguez <grod220@gmail.com>
No description provided.