Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/object-oriented-programming/understand-the-constructor-property.md
2023-07-24 08:34:47 -07:00

2.7 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7daf367417b2b2512b7e Властивість конструктора 1 301327 understand-the-constructor-property

--description--

Існує особлива властивість constructor, що знаходиться в екземплярах об’єктів duck та beagle, які ми створили у попередніх завданнях:

let duck = new Bird();
let beagle = new Dog();

console.log(duck.constructor === Bird); 
console.log(beagle.constructor === Dog);

Обидва виклики console.log виведуть true на консолі.

Зверніть увагу, що властивість constructor є посиланням на функцію-конструктор, яка створила екземпляр. Перевага властивості constructor полягає в тому, що цю властивість можна перевірити та визначити, який це об’єкт. Ось приклад того, як це можна використовувати:

function joinBirdFraternity(candidate) {
  if (candidate.constructor === Bird) {
    return true;
  } else {
    return false;
  }
}

Примітка: оскільки властивість constructor можна перевизначити (детальніше у наступних двох завданнях), для перевірки типу об’єкта краще використовувати метод instanceof.

--instructions--

Напишіть функцію joinDogFraternity, яка приймає параметр candidate та, використовуючи властивість constructor, поверніть true, якщо кандидатом є Dog, в іншому випадку поверніть false.

--hints--

joinDogFraternity потрібно визначити як функцію.

assert(typeof joinDogFraternity === 'function');

joinDogFraternity має повернути true, якщо candidate є екземпляром Dog.

assert(joinDogFraternity(new Dog('')) === true);

joinDogFraternity має використовувати властивість constructor.

assert(/\.constructor/.test(code) && !/instanceof/.test(code));

--seed--

--seed-contents--

function Dog(name) {
  this.name = name;
}

// Only change code below this line
function joinDogFraternity(candidate) {

}

--solutions--

function Dog(name) {
  this.name = name;
}
function joinDogFraternity(candidate) {
  return candidate.constructor === Dog;
}