2.9 KiB
2.9 KiB
id, title, challengeType, forumTopicId, localeTitle
| id | title | challengeType | forumTopicId | localeTitle |
|---|---|---|---|---|
| 587d7daf367417b2b2512b7d | Iterate Over All Properties | 1 | 301320 | Итерация по всем свойствам |
Description
own свойства и свойства prototype . Own свойства определяются непосредственно на самом экземпляре объекта. И prototype определены на prototype . функция Bird (name) {Вот как вы добавляете
this.name = name; //собственность
}
Bird.prototype.numLegs = 2; // свойство прототипа
пусть утка = новая птица («Дональд»);
own свойства duck к массиву ownProps и свойства prototype для массива prototypeProps : let ownProps = [];
let prototypeProps = [];
для (пусть свойство в утке) {
if (duck.hasOwnProperty (свойство)) {
ownProps.push (свойство);
} else {
prototypeProps.push (свойство);
}
}
console.log (ownProps); // печатает ["name"]
console.log (prototypeProps); // печатает ["numLegs"]
Instructions
own свойства beagle в массив ownProps . Добавьте все свойства prototype Dog в массив prototypeProps .
Tests
tests:
- text: The <code>ownProps</code> array should include <code>"name"</code>.
testString: assert(ownProps.indexOf('name') !== -1);
- text: The <code>prototypeProps</code> array should include <code>"numLegs"</code>.
testString: assert(prototypeProps.indexOf('numLegs') !== -1);
- text: Solve this challenge without using the built in method <code>Object.keys()</code>.
testString: assert(!/\Object.keys/.test(code));
Challenge Seed
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
let ownProps = [];
let prototypeProps = [];
// Add your code below this line
Solution
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);
}
}