2018-09-30 23:01:58 +01:00
---
id: 587d7dae367417b2b2512b7b
title: Understand Own Properties
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301326
2021-01-13 03:31:00 +01:00
dashedName: understand-own-properties
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
In the following example, the `Bird` constructor defines two properties: `name` and `numLegs` :
2019-05-17 06:20:30 -07:00
```js
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let duck = new Bird("Donald");
let canary = new Bird("Tweety");
```
2020-11-27 19:02:05 +01: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` :
2019-05-17 06:20:30 -07:00
```js
let ownProps = [];
for (let property in duck) {
if(duck.hasOwnProperty(property)) {
ownProps.push(property);
}
}
console.log(ownProps); // prints [ "name", "numLegs" ]
```
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 the `own` properties of `canary` to the array `ownProps` .
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
`ownProps` should include the values `"numLegs"` and `"name"` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(ownProps.indexOf('name') !== -1 & & ownProps.indexOf('numLegs') !== -1);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should solve this challenge without using the built in method `Object.keys()` .
```js
assert(!/Object(\.keys|\[(['"`])keys\2\])/.test(code));
```
You should solve this challenge without hardcoding the `ownProps` array.
```js
assert(
!/\[\s*(?:'|")(?:name|numLegs)|(?:push|concat)\(\s*(?:'|")(?:name|numLegs)/.test(
code
)
);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
let ownProps = [];
2020-03-08 07:46:28 -07: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
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
function getOwnProps (obj) {
const props = [];
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
props.push(prop);
}
}
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
return props;
}
const ownProps = getOwnProps(canary);
```