-
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?
Conversation
This changes the "Reassigning object data type variables" with revised language and an extended example to explain the concept much more initially clear.
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.
Many thanks, I think the idea behind this definitely makes a clearer distinction between mutation and reassignment. Just a couple of wording changes needed which are detailed below.
Could you also address the two linting errors regarding blank lines? And resolve the merge conflict too?
#### 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 |
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
#### Mutating the properties of an object vs. reassigning the object variables | |
#### Mutation vs. reassignment |
|
||
// 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 comment
The 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)
// Mutating the properties of the object that both 'animal' and 'dog' reference | |
// Mutating the object that both 'animal' and 'dog' reference |
console.log(animal); // { species: "bird" } | ||
console.log(dog); // { species: "cat" } | ||
``` | ||
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 comment
The 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.
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. | |
In the first part, there is only one object which is referenced by both `animal` and `dog`. When we mutate that object, 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. |
|
||
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 | ||
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 comment
The 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 mutate
and reassign
keywords.
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: | |
When multiple variables reference the same object, **mutating** that object will affect all the variables that reference it. However, **reassigning** a variable to a new object will only change the value of that specific variable, and will not automatically reassign any other variable that referenced the original object. For example: |
This changes the "Reassigning object data type variables" with revised language and an extended example to explain the concept much more initially clear.
Because
This changes the "Reassigning object data type variables" with revised language and an extended example to explain the concept much more initially clear.
This PR
Pull Request Requirements
location of change: brief description of change
format, e.g.Intro to HTML and CSS lesson: Fix link text
Because
section summarizes the reason for this PRThis PR
section has a bullet point list describing the changes in this PRIssue
section