freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-an.../object-oriented-programming/define-a-constructor-functi...

3.0 KiB

id title challengeType videoUrl localeTitle
587d7dad367417b2b2512b77 Define a Constructor Function 1 تحديد وظيفة منشئ

Description

Constructors هي وظائف تقوم بإنشاء كائنات جديدة. أنها تحدد الخصائص والسلوكيات التي تنتمي إلى الكائن الجديد. فكر فيها كمخطط لإنشاء كائنات جديدة. هنا مثال constructor :
وظيفة الطيور () {
this.name = "Albert"؛
this.color = "blue"؛
this.numLegs = 2 ،
}
يعرّف هذا constructor كائن Bird name الخاصية ، color ، ومجموعة numLegs إلى Albert ، و blue ، و 2 ، على التوالي. Constructors متابعة بعض الاتفاقيات:
  • يتم تعريف Constructors بالاسم الكبير لتمييزهم عن الوظائف الأخرى غير constructors .
  • Constructors استخدام الكلمة this لتعيين خصائص الكائن فإنها ستخلق. داخل constructor ، يشير this إلى الكائن الجديد الذي سيقوم بإنشائه.
  • Constructors تحديد الخصائص والسلوكيات بدلا من إرجاع قيمة وظائف أخرى القوة.

Instructions

إنشاء constructor ، Dog ، مع name الخصائص ، color ، و numLegs التي تم تعيينها إلى سلسلة ، وسلسلة ، ورقم ، على التوالي.

Tests

tests:
  - text: يجب أن يكون <code>Dog</code> خاصية <code>name</code> معيّنة إلى سلسلة.
    testString: 'assert(typeof (new Dog()).name === "string", "<code>Dog</code> should have a <code>name</code> property set to a string.");'
  - text: يجب أن يكون <code>Dog</code> خاصية <code>color</code> مضبوطة على سلسلة.
    testString: 'assert(typeof (new Dog()).color === "string", "<code>Dog</code> should have a <code>color</code> property set to a string.");'
  - text: يجب أن يكون لدى <code>Dog</code> خاصية <code>numLegs</code> معيّنة إلى رقم.
    testString: 'assert(typeof (new Dog()).numLegs === "number", "<code>Dog</code> should have a <code>numLegs</code> property set to a number.");'

Challenge Seed


Solution

// solution required