Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/object-oriented-programming/verify-an-objects-constructor-with-instanceof.md
2023-07-24 08:34:47 -07:00

2.4 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dae367417b2b2512b7a Перевірка конструктора об’єкта за допомогою instanceof 1 301337 verify-an-objects-constructor-with-instanceof

--description--

Щоразу, коли функція-конструктор створює новий об’єкт, він стає екземпляром конструктора. JavaScript надає зручний спосіб підтвердження за допомогою оператора instanceof. instanceof дозволяє порівняти об’єкт з конструктором та повернути true або false залежно від того, чи був об’єкт створений за допомогою конструктора. Наприклад:

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.

Якщо об’єкт створено без використання конструктора, instanceof підтвердить, що об’єкт не є екземпляром цього конструктора:

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

canary instanceof Bird;

Цей метод instanceof поверне false.

--instructions--

Створіть новий екземпляр конструктора House, назвавши його myHouse та передавши кількість спалень. Потім використайте instanceof, щоб підтвердити, що це екземпляр House.

--hints--

myHouse повинен мати атрибут numBedrooms зі значенням числа.

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

Ви повинні перевірити, що myHouse є екземпляром 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);