2018-10-10 18:03:03 -04:00
---
id: 587d7daf367417b2b2512b7d
2021-02-06 04:42:36 +00:00
title: Iterate Over All Properties
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:15:28 +08:00
forumTopicId: 301320
2021-01-13 03:31:00 +01:00
dashedName: iterate-over-all-properties
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
You have now seen two kinds of properties: `own` properties and `prototype` properties. `Own` properties are defined directly on the object instance itself. And `prototype` properties are defined on the `prototype` .
2020-08-04 15:15:28 +08:00
```js
function Bird(name) {
2021-02-06 04:42:36 +00:00
this.name = name; //own property
2020-08-04 15:15:28 +08:00
}
2021-02-06 04:42:36 +00:00
Bird.prototype.numLegs = 2; // prototype property
2020-08-04 15:15:28 +08:00
let duck = new Bird("Donald");
```
2021-02-06 04:42:36 +00:00
Here is how you add `duck` 's `own` properties to the array `ownProps` and `prototype` properties to the array `prototypeProps` :
2020-08-04 15:15:28 +08:00
```js
let ownProps = [];
let prototypeProps = [];
for (let property in duck) {
if(duck.hasOwnProperty(property)) {
ownProps.push(property);
} else {
prototypeProps.push(property);
}
}
console.log(ownProps); // prints ["name"]
console.log(prototypeProps); // prints ["numLegs"]
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Add all of the `own` properties of `beagle` to the array `ownProps` . Add all of the `prototype` properties of `Dog` to the array `prototypeProps` .
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-02-06 04:42:36 +00:00
The `ownProps` array should only contain `"name"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
2021-02-06 04:42:36 +00:00
assert.deepEqual(ownProps, ['name']);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
The `prototypeProps` array should only contain `"numLegs"` .
2018-10-10 18:03:03 -04:00
```js
2021-02-06 04:42:36 +00:00
assert.deepEqual(prototypeProps, ['numLegs']);
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
You should solve this challenge without using the built in method `Object.keys()` .
2020-08-04 15:15:28 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(!/\Object.keys/.test(code));
2018-10-10 18:03:03 -04:00
```
2020-08-04 15:15:28 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
let ownProps = [];
let prototypeProps = [];
// Only change code below this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
let ownProps = [];
let prototypeProps = [];
for (let prop in beagle) {
if (beagle.hasOwnProperty(prop)) {
ownProps.push(prop);
} else {
prototypeProps.push(prop);
}
}
```