Mutability
🔍: Let's delve into the concept of mutability in JavaScript, as described in the excerpt from "Eloquent JavaScript."
👩🎓: Understanding Mutability:
Immutable vs. Mutable Types: In JavaScript, basic data types like numbers, strings, and Booleans are immutable. This means you cannot alter these types of values once they are created. For example, if you have a string "cat," you cannot change it to "rat" directly; you would have to create a new string.
Objects and Mutability: Objects in JavaScript are mutable. This means you can change the properties of an object after it has been created. The object itself can have different contents at different times.
🎨: Demonstrating Object Mutability with an Example:
Creating and Modifying Objects:
When
object1is created with{value: 10}, and thenobject2is set toobject1, bothobject1andobject2refer to the same object. Therefore, a change inobject1will be reflected inobject2.However,
object3is a new object with the same properties but is distinct fromobject1. Changes toobject1won't affectobject3.
Comparing Objects:
object1 == object2returnstruebecause they reference the same object.object1 == object3returnsfalsebecause, despite having identical properties, they are two different objects.
Identity vs. Equality:
When objects are compared using
==, it checks for identity, not for equality of content. Two objects are considered identical if they refer to the same underlying object.
🔬: Constant Bindings and Objects:
Even if an object is bound to a constant (using
const), the object's contents can still be changed. What cannot change is the binding itself; you can't reassign a new object to that constant. For example, modifyingscore.visitorsis allowed, but trying to assign a new object toscoreis not.
⚙️: Deep Comparison:
JavaScript doesn't have a built-in operation for "deep" comparison (comparing objects based on their content). However, such a comparison can be implemented manually. This involves comparing each property of the objects to determine if their contents are identical.
📚: In summary, understanding the difference between mutable and immutable types, and the concept of object identity versus equality, is crucial in JavaScript programming. This knowledge is fundamental when manipulating objects and understanding how changes to these objects can affect your program's state and behavior.
Code explanation:
let object1 = {value: 10};
let object2 = object1;
let object3 = {value: 10};
console.log(object1 == object2);
// → true
console.log(object1 == object3);
// → false
object1.value = 15;
console.log(object2.value);
// → 15
console.log(object3.value);
// → 10
This piece of code is demonstrating how objects in JavaScript are compared and how they interact when assigned to variables. Let's break it down step by step:
Creating Objects:
let object1 = {value: 10};: This line creates an objectobject1with a propertyvalueset to 10.let object2 = object1;: Here,object2is assigned the same reference asobject1. They both point to the same object in memory.let object3 = {value: 10};: This creates a new objectobject3with the same propertyvalueset to 10, but it's a different object in memory compared toobject1.
Comparing Objects:
console.log(object1 == object2);: This checks ifobject1andobject2are the same. It returnstruebecauseobject2was assigned the reference ofobject1, meaning they both point to the same object in memory.console.log(object1 == object3);: This comparesobject1andobject3. It returnsfalsebecause, even though they have the same property values, they are different objects stored at different locations in memory.
Modifying Object Properties:
object1.value = 15;: This line changes thevalueproperty ofobject1to 15. Sinceobject2refers to the same object asobject1, this change will also be reflected inobject2.console.log(object2.value);: Outputs15becauseobject2is linked toobject1, and thevalueproperty ofobject1was changed to 15.console.log(object3.value);: Outputs10becauseobject3is a separate object, and its properties are independent of changes made toobject1.
In summary, object1 and object2
are references to the same object, so changes to one affect the other
and they are considered equal. object3,
however, is a distinct object with the same properties but stored in
a different memory location, so it's not affected by changes to
object1 and is not considered equal to
object1 or object2.
Code explanation:
const score = {visitors: 0, home: 0};
// This is okay
score.visitors = 1;
// This isn't allowed
score = {visitors: 1, home: 1};
This code snippet demonstrates the concept of mutability in the context of a constant object in JavaScript. Let's examine it in detail:
Creating a Constant Object:
const score = {visitors: 0, home: 0};: Here, a constant objectscoreis created with two properties:visitorsandhome, both initialized to 0. Sincescoreis declared withconst, it means you cannot reassignscoreto a different value or object. However, the properties of the objectscoreitself are mutable (they can be changed).
Modifying Object Properties:
score.visitors = 1;: This line is modifying the propertyvisitorsof thescoreobject by setting it to 1. This is allowed because you are changing the property of the object, not the objectscoreitself.
Attempting to Reassign the Constant Object:
score = {visitors: 1, home: 1};: This line is trying to reassign the entirescoreobject to a new object. This is not allowed in JavaScript when the variable is declared withconst. Theconstdeclaration ensures that the variablescorealways refers to the same object, and this line attempts to change that reference to a new object, which is why it results in an error.
In summary, when you declare an object with const,
you can modify the properties of the object, but you cannot reassign
the object to a new object. This is a key aspect of how const
works in JavaScript with objects.
Link to the chapter in the book Eloquent Javascript:
Comments
Post a Comment