2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 56bbb991ad1ed5201cd392d3
|
2021-03-14 21:20:39 -06:00
|
|
|
title: 删除对象的属性
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/cDqKdTv'
|
|
|
|
forumTopicId: 17560
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: delete-properties-from-a-javascript-object
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
我们同样可以删除对象的属性,例如:
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-04-06 00:04:04 +09:00
|
|
|
```js
|
|
|
|
delete ourDog.bark;
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
例如:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const ourDog = {
|
2021-02-06 04:42:36 +00:00
|
|
|
"name": "Camper",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["everything!"],
|
|
|
|
"bark": "bow-wow"
|
|
|
|
};
|
|
|
|
|
|
|
|
delete ourDog.bark;
|
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
在上面代码的最后一行中,`ourDog` 是这样的:
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
{
|
|
|
|
"name": "Camper",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["everything!"]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --instructions--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
删除 `myDog` 对象的 `tails` 属性。 可以使用点操作符或者中括号操作符。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
应该从 `myDog` 中删除 `tails` 属性。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(typeof myDog === 'object' && myDog.tails === undefined);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-03-14 21:20:39 -06:00
|
|
|
不要修改 `myDog` 的初始化代码。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2021-02-06 04:42:36 +00:00
|
|
|
assert(code.match(/"tails": 1/g).length > 0);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --after-user-code--
|
|
|
|
|
|
|
|
```js
|
|
|
|
(function(z){return z;})(myDog);
|
|
|
|
```
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Setup
|
2021-10-27 15:10:57 +00:00
|
|
|
const myDog = {
|
2021-01-13 03:31:00 +01:00
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"],
|
|
|
|
"bark": "woof"
|
|
|
|
};
|
|
|
|
|
|
|
|
// Only change code below this line
|
2021-10-27 15:10:57 +00:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```js
|
2021-10-27 15:10:57 +00:00
|
|
|
const myDog = {
|
2021-01-13 03:31:00 +01:00
|
|
|
"name": "Happy Coder",
|
|
|
|
"legs": 4,
|
|
|
|
"tails": 1,
|
|
|
|
"friends": ["freeCodeCamp Campers"],
|
|
|
|
"bark": "woof"
|
|
|
|
};
|
|
|
|
delete myDog.tails;
|
|
|
|
```
|