2018-10-10 18:03:03 -04:00
---
id: 587d7dae367417b2b2512b7b
2021-02-06 04:42:36 +00:00
title: Understand Own Properties
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-04 15:15:28 +08:00
forumTopicId: 301326
2021-01-13 03:31:00 +01:00
dashedName: understand-own-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
In the following example, the `Bird` constructor defines two properties: `name` and `numLegs` :
2020-08-04 15:15:28 +08:00
```js
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let duck = new Bird("Donald");
let canary = new Bird("Tweety");
```
2021-02-06 04:42:36 +00:00
`name` and `numLegs` are called `own` properties, because they are defined directly on the instance object. That means that `duck` and `canary` each has its own separate copy of these properties. In fact every instance of `Bird` will have its own copy of these properties. The following code adds all of the `own` properties of `duck` to the array `ownProps` :
2020-08-04 15:15:28 +08:00
```js
let ownProps = [];
for (let property in duck) {
if(duck.hasOwnProperty(property)) {
ownProps.push(property);
}
}
console.log(ownProps); // prints [ "name", "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 the `own` properties of `canary` to the array `ownProps` .
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
`ownProps` should include the values `"numLegs"` and `"name"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(ownProps.indexOf('name') !== -1 & & ownProps.indexOf('numLegs') !== -1);
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()` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(!/Object(\.keys|\[(['"`])keys\2\])/.test(code));
2018-10-10 18:03:03 -04:00
```
2020-12-16 00:37:30 -07:00
You should solve this challenge without hardcoding the `ownProps` array.
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(
!/\[\s*(?:'|")(?:name|numLegs)|(?:push|concat)\(\s*(?:'|")(?:name|numLegs)/.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 Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
let ownProps = [];
// 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 Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
function getOwnProps (obj) {
const props = [];
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
props.push(prop);
}
}
return props;
}
const ownProps = getOwnProps(canary);
```