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
2018-09-30 23:01:58 +01:00
---
## Description
< section id = 'description' >
In the following example, the < code > Bird< / code > constructor defines two properties: < code > name< / code > and < code > numLegs< / code > :
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");
```
2018-09-30 23:01:58 +01:00
< code > name< / code > and < code > numLegs< / code > are called < code > own< / code > properties, because they are defined directly on the instance object. That means that < code > duck< / code > and < code > canary< / code > each has its own separate copy of these properties.
In fact every instance of < code > Bird< / code > will have its own copy of these properties.
The following code adds all of the < code > own< / code > properties of < code > duck< / code > to the array < code > ownProps< / code > :
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" ]
```
2018-09-30 23:01:58 +01:00
< / section >
## Instructions
< section id = 'instructions' >
Add the < code > own< / code > properties of < code > canary< / code > to the array < code > ownProps< / code > .
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: < code > ownProps</ code > should include the values < code > "numLegs"</ code > and < code > "name"</ code > .
2019-03-17 15:47:41 -07:00
testString: assert(ownProps.indexOf('name') !== -1 & & ownProps.indexOf('numLegs') !== -1);
2019-11-27 02:57:38 -08:00
- text: You should solve this challenge without using the built in method < code > Object.keys()</ code > .
2019-03-17 15:47:41 -07:00
testString: assert(!/Object(\.keys|\[(['"`])keys\2\])/.test(code));
2019-11-27 02:57:38 -08:00
- text: You should solve this challenge without hardcoding the < code > ownProps</ code > array.
2019-03-17 15:47:41 -07:00
testString: assert(!/\[\s*(?:'|")(?:name|numLegs)|(?:push|concat)\(\s*(?:'|")(?:name|numLegs)/.test(code));
2018-09-30 23:01:58 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```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
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```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);
```
< / section >