freeCodeCamp/curriculum/challenges/russian/02-javascript-algorithms-an.../object-oriented-programming/understand-the-constructor-...

2.8 KiB

id title challengeType videoUrl localeTitle
587d7daf367417b2b2512b7e Understand the Constructor Property 1 Понять свойство конструктора

Description

Существует специальное свойство constructor расположенное на объектных экземплярах duck и beagle которые были созданы в предыдущих задачах:
let duck = new Bird ();
let beagle = new Dog ();

console.log (duck.constructor === Bird); // выводит true
console.log (beagle.constructor === Собака); // выводит true
Обратите внимание, что свойство constructor является ссылкой на конструктор, создавший экземпляр. Преимущество свойства constructor заключается в том, что можно проверить это свойство, чтобы узнать, какой он объект. Вот пример того, как это можно использовать:
function joinBirdFraternity (кандидат) {
if (кандидат.конструктор === Птица) {
return true;
} else {
return false;
}
}
Заметка
Поскольку свойство constructor может быть перезаписано (что будет рассмотрено в следующих двух задачах), лучше всего использовать метод instanceof для проверки типа объекта.

Instructions

undefined

Tests

tests:
  - text: <code>joinDogFraternity</code> следует определять как функцию.
    testString: 'assert(typeof(joinDogFraternity) === "function", "<code>joinDogFraternity</code> should be defined as a function.");'
  - text: ''
    testString: 'assert(joinDogFraternity(new Dog("")) === true, "<code>joinDogFraternity</code> should return true if<code>candidate</code> is an instance of <code>Dog</code>.");'
  - text: <code>joinDogFraternity</code> должен использовать свойство <code>constructor</code> .
    testString: 'assert(/\.constructor/.test(code) && !/instanceof/.test(code), "<code>joinDogFraternity</code> should use the <code>constructor</code> property.");'

Challenge Seed

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

// Add your code below this line
function joinDogFraternity(candidate) {

}

Solution

// solution required