1.8 KiB
1.8 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 587d7db1367417b2b2512b86 | 重置一個繼承的構造函數屬性 | 1 | 301324 | reset-an-inherited-constructor-property |
--description--
當一個對象從另一個對象那裏繼承了其 prototype 時,那它也繼承了父類的 constructor 屬性。
請看下面的舉例:
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
let duck = new Bird();
duck.constructor
但是 duck 和其他所有 Bird 的實例都應該表明它們是由 Bird 創建的,而不是由 Animal 創建的。 爲此,你可以手動將 Bird 的構造函數屬性設置爲 Bird 對象:
Bird.prototype.constructor = Bird;
duck.constructor
--instructions--
修改你的代碼,使得 duck.constructor 和 beagle.constructor 返回各自的構造函數。
--hints--
Bird.prototype 應該是 Animal 的一個實例。
assert(Animal.prototype.isPrototypeOf(Bird.prototype));
duck.constructor 應該返回 Bird。
assert(duck.constructor === Bird);
Dog.prototype 應該是 Animal 的一個實例。
assert(Animal.prototype.isPrototypeOf(Dog.prototype));
beagle.constructor 應該返回 Dog。
assert(beagle.constructor === Dog);
--seed--
--seed-contents--
function Animal() { }
function Bird() { }
function Dog() { }
Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);
// Only change code below this line
let duck = new Bird();
let beagle = new Dog();
--solutions--
function Animal() { }
function Bird() { }
function Dog() { }
Bird.prototype = Object.create(Animal.prototype);
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;
Bird.prototype.constructor = Bird;
let duck = new Bird();
let beagle = new Dog();