2.4 KiB
2.4 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7daf367417b2b2512b7d | すべてのプロパティを反復処理する | 1 | 301320 | iterate-over-all-properties |
--description--
ここまで、独自のプロパティと prototype
プロパティの 2 種類のプロパティについて説明しました。 独自のプロパティは、オブジェクトインスタンス自体に直接定義されます。 prototype プロパティは 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
に追加してください。 Dog
のすべての prototype
プロパティを配列 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);
}
}