2.8 KiB
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