Files
freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-and-data-structures/object-oriented-programming/use-a-constructor-to-create-objects.md
2022-10-20 09:13:17 -07:00

1.9 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dad367417b2b2512b78 Use a Constructor to Create Objects 1 18233 use-a-constructor-to-create-objects

--description--

إليك constructor الـ Bird من التحدي السابق:

function Bird() {
  this.name = "Albert";
  this.color  = "blue";
  this.numLegs = 2;
}

let blueBird = new Bird();

ملاحظة: كلمة this داخل الـ constructor تشير دائما إلى الـ object الذي يتم إنشاؤه.

لاحظ أن مشغل new يستخدم عند استدعاء الـ constructor. هذا يخبر جافا سكريبت بإنشاء instance جديد من Bird يسمى blueBird. بدون مشغل new، كلمة this داخل الـ constructor لن تشير إلى ال object المنشأ حديثا، مما يعطي نتائج غير متوقعة. الآن blueBird لديه جميع الخصائص المرفة داخل constructor الـ Bird:

blueBird.name;
blueBird.color;
blueBird.numLegs;

تماما مثل أي object آخر، يمكن الوصول إلى خصائصه وتعديلها:

blueBird.name = 'Elvira';
blueBird.name;

--instructions--

استخدم constructor الـ Dog من الدرس الأخير لإنشاء instance جديد من Dog و تعيينه إلى متغير hound.

--hints--

hound يجب أن يتم إنشاؤه باستخدام constructor الـ Dog.

assert(hound instanceof Dog);

الكود الخاص بك يجب أن يستخدم مشغل new لإنشاء instance الـ Dog.

assert(code.match(/new/g));

--seed--

--seed-contents--

function Dog() {
  this.name = "Rupert";
  this.color = "brown";
  this.numLegs = 4;
}
// Only change code below this line

--solutions--

function Dog() {
  this.name = "Rupert";
  this.color = "brown";
  this.numLegs = 4;
}
const hound = new Dog();