Skip to content

Week3 homework #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
finished 2.7
  • Loading branch information
jshortz committed Jul 13, 2019
commit cc6b2f9456687c571649dd7e65f66f729e27ad3c
6 changes: 5 additions & 1 deletion Week3/homework/step2-7.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ f2(y);

console.log(y);

// Add your explanation as a comment here
/* In the x-code, we are logging the value of x, which has not changed. If we want this code to return 10,
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 } */
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in the objects example, both a and b would have a value of

{
name: 'gabe',
country: 'us'
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy