prototype
,它也继承了supertype
的构造函数属性。这是一个例子: 函数Bird(){}但
Bird.prototype = Object.create(Animal.prototype);
let duck = new Bird();
duck.constructor // function Animal(){...}
duck
和所有Bird
实例都应该表明它们是由Bird
而不是Animal
建造的。为此,您可以手动将Bird's
构造函数属性设置为Bird
对象: Bird.prototype.constructor = Bird;
duck.constructor // function Bird(){...}
duck.constructor
和beagle.constructor
返回各自的构造函数。 Bird.prototype
应该是Animal
一个实例。
testString: 'assert(Animal.prototype.isPrototypeOf(Bird.prototype), "Bird.prototype
should be an instance of Animal
.");'
- text: duck.constructor
应该返回Bird
。
testString: 'assert(duck.constructor === Bird, "duck.constructor
should return Bird
.");'
- text: Dog.prototype
应该是Animal
一个实例。
testString: 'assert(Animal.prototype.isPrototypeOf(Dog.prototype), "Dog.prototype
should be an instance of Animal
.");'
- text: beagle.constructor
应该返回Dog
。
testString: 'assert(beagle.constructor === Dog, "beagle.constructor
should return Dog
.");'
```