* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
1.9 KiB
1.9 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7db1367417b2b2512b86 | 重置一个继承的构造函数属性 | 1 | 301324 | reset-an-inherited-constructor-property |
--description--
当一个对象从另一个对象那里继承了其原型
,那它也继承了父类
的 constructor 属性。
请看下面的举例:
function Bird() { }
Bird.prototype = Object.create(Animal.prototype);
let duck = new Bird();
duck.constructor // function Animal(){...}
但是duck
和其他所有Bird
的实例都应该表明它们是由Bird
创建的,而不是由Animal
创建的。为此,你可以手动把Bird
的 constructor 属性设置为Bird
对象:
Bird.prototype.constructor = Bird;
duck.constructor // function Bird(){...}
--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();