Files
freeCodeCamp/curriculum/challenges/arabic/02-javascript-algorithms-and-data-structures/object-oriented-programming/verify-an-objects-constructor-with-instanceof.md
2022-10-20 09:13:17 -07:00

2.1 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dae367417b2b2512b7a Verify an Object's Constructor with instanceof 1 301337 verify-an-objects-constructor-with-instanceof

--description--

في أي وقت تقوم الـ constructor function بإنشاء object جديد، يقال إن هذا ال object هو instance من الـ constructor الخاص به. يعطي جافا سكريبت طريقة مناسبة للتحقق من هذا مع مشغل instanceof. instanceof يسمح لك بمقارنة object مع constructor، و إرجاع true أو false استناداً إلى ما إذا كان هذا ال object قد تم إنشاؤه باستخدام الـ constructor أم لا. إليك مثال:

let Bird = function(name, color) {
  this.name = name;
  this.color = color;
  this.numLegs = 2;
}

let crow = new Bird("Alexis", "black");

crow instanceof Bird;

دالة instanceof هنا، ستعيد true.

إذا تم إنشاء object دون استخدام constructor، فـ instanceof سيتحقق من أنه ليس instance لذلك الـ constructor:

let canary = {
  name: "Mildred",
  color: "Yellow",
  numLegs: 2
};

canary instanceof Bird;

دالة instanceof هنا، ستعيد false.

--instructions--

قم بإنشاء instance جديد لـ House ، واسمه myHouse و مرر اليه عدد من غرف النوم. ثم استخدم instanceof للتحقق من أنه instance لـ House.

--hints--

يجب أن يحتوي myHouse على سمة numBedrooms معينة إلى رقم.

assert(typeof myHouse.numBedrooms === 'number');

يجب عليك التحقق من أن myHouse هو instance لـ House باستخدام مشغل instanceof.

assert(/myHouse\s*instanceof\s*House/.test(code));

--seed--

--seed-contents--

function House(numBedrooms) {
  this.numBedrooms = numBedrooms;
}

// Only change code below this line

--solutions--

function House(numBedrooms) {
  this.numBedrooms = numBedrooms;
}
const myHouse = new House(4);
console.log(myHouse instanceof House);