Updating Object Properties:
After you've created a JavaScript object, you can update its properties at any time just like you would update any other variable. You can use either dot or bracket notation to update.
For example, let's look at ourDog:
const ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
Since he's a particularly happy dog, let's change his name to the string Happy Camper. Here's how we update his object's name property: ourDog.name = "Happy Camper"; or ourDog["name"] = "Happy Camper";
Now when we evaluate ourDog.name, instead of getting Camper, we'll get his new name, Happy Camper.
Add New Properties to a JavaScript Object:
You can add new properties to existing JavaScript objects the same way you would modify them.
Here's how we would add a bark property to ourDog:
ourDog.bark = "bow-wow";
or
ourDog["bark"] = "bow-wow";
Now when we evaluate ourDog.bark, we'll get his bark, bow-wow.
Example:
const ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
};
ourDog.bark = "bow-wow";
Delete Properties from a JavaScript Object:
We can also delete properties from objects like this:
delete ourDog.bark;
Example:
const ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"],
"bark": "bow-wow"
};
delete ourDog.bark;
After the last line shown above, ourDog looks like:
{
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"]
}
No comments:
Post a Comment