-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Object basics: Clearer wording/example with mutations & references #29846
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -86,20 +86,26 @@ | |||||
|
||||||
<div class="lesson-note" markdown="1"> | ||||||
|
||||||
#### Reassigning object data type variables | ||||||
|
||||||
While mutating the object we have a reference to affects all other variables that reference it, reassigning a variable does not change what the other variables refer to. For example: | ||||||
#### Mutating the properties of an object vs. reassigning the object variables | ||||||
Check failure on line 89 in foundations/javascript_basics/object_basics.md
|
||||||
When you have multiple variables that reference the same object, mutating the properties of that object will affect all the variables that reference it. However, reassigning the object variable to a new object will only change the value of that specific variable, and will not affect the other variables that previously referenced the original object. For example: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just some small wording tweak given the rest of the review comments. Strength added to the
Suggested change
|
||||||
|
||||||
```javascript | ||||||
let animal = { species: "dog" }; | ||||||
let dog = animal; | ||||||
|
||||||
// reassigning animal variable with a completely new object | ||||||
animal = { species: "cat" }; | ||||||
// Mutating the properties of the object that both 'animal' and 'dog' reference | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The object is being mutated, not the property (which is actually being reassigned)
Suggested change
|
||||||
animal.species = "cat"; | ||||||
|
||||||
console.log(animal); // { species: "cat" } | ||||||
console.log(dog); // { species: "dog" } | ||||||
console.log(dog); // { species: "cat" } | ||||||
|
||||||
// Reassigning the 'animal' variable to a new object | ||||||
animal = { species: "bird" }; | ||||||
|
||||||
console.log(animal); // { species: "bird" } | ||||||
console.log(dog); // { species: "cat" } | ||||||
``` | ||||||
Check failure on line 107 in foundations/javascript_basics/object_basics.md
|
||||||
In the first part, when we modify the species property of the object that both `animal` and `dog` reference, the change is reflected in both variables. In the second part, when we reassign the `animal` variable to a new object, the `dog` variable still references the original object, while `animal` now points to the new object. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixing with the same as the previous review comment, and a small re-wording to accommodate that.
Suggested change
|
||||||
|
||||||
</div> | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the new heading is a little verbose for a heading