mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-24 11:03:17 -04:00
2.4 KiB
2.4 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7daf367417b2b2512b7d | Ітерація усіх властивостей | 1 | 301320 | iterate-over-all-properties |
--description--
Тепер ви вже бачили два типи властивостей: own properties and prototype properties. Власні властивості визначаються безпосередньо в самому екземплярі об'єкта. А властивості прототипу визначені в prototype.
function Bird(name) {
this.name = name; //own property
}
Bird.prototype.numLegs = 2; // prototype property
let duck = new Bird("Donald");
Ось як ви додаєте власні властивості duck до масиву ownProps і властивостей prototype до масиву prototypeProps:
let ownProps = [];
let prototypeProps = [];
for (let property in duck) {
if(duck.hasOwnProperty(property)) {
ownProps.push(property);
} else {
prototypeProps.push(property);
}
}
console.log(ownProps);
console.log(prototypeProps);
console.log(ownProps) відобразить ["name"] в консолі, і console.log(prototypeProps) відображатиме ["numLegs"].
--instructions--
Додайте всі власні властивості beagle до масиву ownProps. Додайте всі властивості prototype Dog до масиву prototypeProps.
--hints--
Масив ownProps повинен містити лише name.
assert.deepEqual(ownProps, ['name']);
Масив prototypeProps повинен містити лише numLegs.
assert.deepEqual(prototypeProps, ['numLegs']);
Ви повинні вирішити цей виклик без використання побудови в методі Object.keys().
assert(!/\Object.keys/.test(code));
--seed--
--seed-contents--
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
--solutions--
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);
}
}