2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: 56bbb991ad1ed5201cd392d2
|
|
|
|
title: Add New Properties to a JavaScript Object
|
|
|
|
challengeType: 1
|
2019-02-14 12:24:02 -05:00
|
|
|
videoUrl: 'https://scrimba.com/c/cQe38UD'
|
2019-08-05 09:17:33 -07:00
|
|
|
forumTopicId: 301169
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
You can add new properties to existing JavaScript objects the same way you would modify them.
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
|
|
Here's how we would add a `"bark"` property to `ourDog`:
|
|
|
|
|
|
|
|
`ourDog.bark = "bow-wow";`
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
or
|
2020-11-27 19:02:05 +01:00
|
|
|
|
|
|
|
`ourDog["bark"] = "bow-wow";`
|
|
|
|
|
|
|
|
Now when we evaluate `ourDog.bark`, we'll get his bark, "bow-wow".
|
2020-03-25 08:07:13 -07:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```js
|
|
|
|
var ourDog = {
|
|
|
|
"name": "Camper",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["everything!"]
|
|
|
|
};
|
|
|
|
|
|
|
|
ourDog.bark = "bow-wow";
|
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --instructions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
Add a `"bark"` property to `myDog` and set it to a dog sound, such as "woof". You may use either dot or bracket notation.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --hints--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
You should add the property `"bark"` to `myDog`.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(myDog.bark !== undefined);
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
You should not add `"bark"` to the setup section.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(!/bark[^\n]:/.test(code));
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(z){return z;})(myDog);
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
|
|
|
var myDog = {
|
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"]
|
|
|
|
};
|
|
|
|
|
2020-03-02 23:18:30 -08:00
|
|
|
// Only change code below this line
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --solutions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
var myDog = {
|
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"]
|
|
|
|
};
|
|
|
|
myDog.bark = "Woof Woof";
|
|
|
|
```
|