mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-01-04 09:05:49 -05:00
2.4 KiB
2.4 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7dae367417b2b2512b7b | فهم الخصائص المملوكة (Understand Own Properties) | 1 | 301326 | understand-own-properties |
--description--
في المثال التالي، يقوم constructor الـ Bird بتحديد خاصيتين: name و numLegs:
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let duck = new Bird("Donald");
let canary = new Bird("Tweety");
name و numLegs تسمى own properties، لأنها تعرف مباشرة على الـ instance object. وهذا يعني أن كل من duck و canary لديهم نسخة منفصلة خاصة بهم من هذه الخواص. في الواقع كل instance من Bird سيكون لديها نسخة خاصة بها من هذه الخواص. الكود التالي يضيف جميع الخصائص الخاصة بـ duck إلى مصفوفة ownProps:
let ownProps = [];
for (let property in duck) {
if(duck.hasOwnProperty(property)) {
ownProps.push(property);
}
}
console.log(ownProps);
ستعرض وحدة التحكم القيمة ["name", "numLegs"].
--instructions--
أضف جميع الخصائص الخاصة بـ canary إلى الـ array الآتية ownProps.
--hints--
ownProps يجب أن تتضمن القيم numLegs و name.
assert(ownProps.indexOf('name') !== -1 && ownProps.indexOf('numLegs') !== -1);
يجب عليك حل هذا التحدي دون استخدام الدالة المدمجة في Object.keys().
assert(!/Object(\.keys|\[(['"`])keys\2\])/.test(code));
يجب عليك حل هذا التحدي بدون إدخال قيم مثبتة (hard-coding) في قائمة ownProps.
assert(
!/\[\s*(?:'|")(?:name|numLegs)|(?:push|concat)\(\s*(?:'|")(?:name|numLegs)/.test(
code
)
);
--seed--
--seed-contents--
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
let ownProps = [];
// Only change code below this line
--solutions--
function Bird(name) {
this.name = name;
this.numLegs = 2;
}
let canary = new Bird("Tweety");
function getOwnProps (obj) {
const props = [];
for (let prop in obj) {
if (obj.hasOwnProperty(prop)) {
props.push(prop);
}
}
return props;
}
const ownProps = getOwnProps(canary);