2.5 KiB
2.5 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7dad367417b2b2512b78 | Use a Constructor to Create Objects | 1 | استخدم منشئ لإنشاء كائنات |
Description
Bird
من التحدي السابق: وظيفة الطيور () {لاحظ أن المشغل
this.name = "Albert"؛
this.color = "blue"؛
this.numLegs = 2 ،
// "هذا" داخل المنشئ يشير دائمًا إلى الكائن الذي يتم إنشاؤه
}
واسمحوا blueBird = الطيور الجديدة () ؛
new
يستخدم عند استدعاء منشئ. هذا يخبر JavaScript لإنشاء instance
جديد من Bird
باسم blueBird
. بدون المشغل new
، لا يشير this
داخل المُنشئ إلى الكائن الذي تم إنشاؤه حديثًا ، مما يعطي نتائج غير متوقعة. الآن blueBird
لديه كل الخصائص التي تم تعريفها داخل منشئ Bird
: blueBird.name. // => ألبرتتمامًا مثل أي كائن آخر ، يمكن الوصول إلى خصائصه وتعديلها:
blueBird.color. // => أزرق
blueBird.numLegs. // => 2
blueBird.name = 'Elvira' ،
blueBird.name. // => إلفيرا
Instructions
undefined
Tests
tests:
- text: يجب إنشاء <code>hound</code> باستخدام منشئ <code>Dog</code> .
testString: 'assert(hound instanceof Dog, "<code>hound</code> should be created using the <code>Dog</code> constructor.");'
- text: يجب أن تستخدم شفرتك المشغل <code>new</code> لإنشاء <code>instance</code> <code>Dog</code> .
testString: 'assert(code.match(/new/g), "Your code should use the <code>new</code> operator to create an <code>instance</code> of <code>Dog</code>.");'
Challenge Seed
function Dog() {
this.name = "Rupert";
this.color = "brown";
this.numLegs = 4;
}
// Add your code below this line
Solution
// solution required