Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/object-oriented-programming/verify-an-objects-constructor-with-instanceof.md

2.3 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--

Щоразу, коли конструктор функцій створює новий об'єкт, він стає instance для його конструктора. 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);