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
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: add-new-properties-to-a-javascript-object
|
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
|
|
|
|
2021-03-02 16:12:12 -08:00
|
|
|
Here's how we would add a `bark` property to `ourDog`:
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2021-03-25 15:07:48 -06:00
|
|
|
```js
|
|
|
|
ourDog.bark = "bow-wow";
|
|
|
|
```
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
or
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2021-03-25 15:07:48 -06:00
|
|
|
```js
|
|
|
|
ourDog["bark"] = "bow-wow";
|
|
|
|
```
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2021-03-02 16:12:12 -08:00
|
|
|
Now when we evaluate `ourDog.bark`, we'll get his bark, `bow-wow`.
|
2020-03-25 08:07:13 -07:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```js
|
2021-10-26 01:55:58 +09:00
|
|
|
const ourDog = {
|
2020-03-25 08:07:13 -07:00
|
|
|
"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
|
|
|
|
2021-03-02 16:12:12 -08: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
|
|
|
|
2021-03-02 16:12:12 -08: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
|
|
|
```
|
|
|
|
|
2021-04-08 12:49:47 -05:00
|
|
|
You should not add `bark` to the initialization of `myDog`.
|
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
|
2021-10-26 01:55:58 +09:00
|
|
|
const myDog = {
|
2018-09-30 23:01:58 +01:00
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"]
|
|
|
|
};
|
|
|
|
|
2021-04-08 12:49:47 -05:00
|
|
|
|
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
|
2021-10-26 01:55:58 +09:00
|
|
|
const myDog = {
|
2018-09-30 23:01:58 +01:00
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"]
|
|
|
|
};
|
|
|
|
myDog.bark = "Woof Woof";
|
|
|
|
```
|