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
для проверки типа объекта.
joinDogFraternity
function that takes a candidate
parameter and, using the constructor
property, return true
if the candidate is a Dog
, otherwise return false
.
joinDogFraternity
should be defined as a function.
testString: assert(typeof(joinDogFraternity) === 'function');
- text: joinDogFraternity
should return true ifcandidate
is an instance of Dog
.
testString: assert(joinDogFraternity(new Dog("")) === true);
- text: joinDogFraternity
should use the constructor
property.
testString: assert(/\.constructor/.test(code) && !/instanceof/.test(code));
```