'bambi'对象的Constructor怎么是Animal?
How the Constructor of the 'bambi' object is Animal?
let log = console.log;
function Animal(name)
{
this.name = name;
}
Animal.prototype.walk = function(){
log(`${this.name} walks`);
}
function Cat(name)
{
Animal.call(this,name);
this.lives = 9;
}
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.meow = function(){
log("Meow!");
}
let bambi = new Cat("Bambi");
log(bambi.constructor) // function Animal(name){ this.name = name; }
我已经使用 Cat 构造函数创建了 'bambi' 对象,但在检查 bambi 的构造函数时它 returns Animal 构造函数。我已经使用 Object.create() 将 Cat 的原型分配给了 Animal 原型。为什么bambi对象的构造函数是Animal而不是Cat?
查找“javascript 没有 class 的继承”
在 MDN page 上,它是这样写的:
- Add the following line below your previous addition:
Teacher.prototype = Object.create(Person.prototype);
Copy to Clipboard
Here our friend create() comes to the rescue again. In this case we are using it to create a new object and make it the value of Teacher.prototype. The new object has Person.prototype as its prototype and will therefore inherit, if and when needed, all the methods available on Person.prototype.
- We need to do one more thing before we move on. After adding the last line, the constructor property of Teacher.prototype is now equal to Person(), because we just set Teacher.prototype to reference an object that inherits its properties from Person.prototype! Try saving your code, loading the page in a browser, and entering Teacher.prototype.constructor into the console to verify.
- This can become a problem, so we need to set this right. You can do so by going back to your source code and adding the following line at the bottom:
Object.defineProperty(Teacher.prototype, 'constructor', {
value: Teacher,
enumerable: false, // so that it does not appear in 'for in' loop
writable: true });
- Now if you save and refresh, entering Teacher.prototype.constructor should return Teacher(), as desired, plus we are now inheriting from Person()!
因此,基于此,您的代码需要添加以下小部分:
Object.defineProperty(Cat.prototype, 'constructor', {
value: Cat,
enumerable: false, // so that it does not appear in 'for in' loop
writable: true });
请参阅下面的工作演示:
let log = console.log;
function Animal(name)
{
this.name = name;
}
Animal.prototype.walk = function(){
log(`${this.name} walks`);
}
function Cat(name)
{
Animal.call(this,name);
this.lives = 9;
}
Cat.prototype = Object.create(Animal.prototype);
Object.defineProperty(Cat.prototype, 'constructor', {
value: Cat,
enumerable: false, // so that it does not appear in 'for in' loop
writable: true });
Cat.prototype.meow = function(){
log("Meow!");
}
let bambi = new Cat("Bambi");
log(bambi.constructor);
但是,我建议只使用 ES6 classes 而不是手动执行此操作
let log = console.log;
function Animal(name)
{
this.name = name;
}
Animal.prototype.walk = function(){
log(`${this.name} walks`);
}
function Cat(name)
{
Animal.call(this,name);
this.lives = 9;
}
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.meow = function(){
log("Meow!");
}
let bambi = new Cat("Bambi");
log(bambi.constructor) // function Animal(name){ this.name = name; }
我已经使用 Cat 构造函数创建了 'bambi' 对象,但在检查 bambi 的构造函数时它 returns Animal 构造函数。我已经使用 Object.create() 将 Cat 的原型分配给了 Animal 原型。为什么bambi对象的构造函数是Animal而不是Cat?
查找“javascript 没有 class 的继承”
在 MDN page 上,它是这样写的:
- Add the following line below your previous addition:
Teacher.prototype = Object.create(Person.prototype);
Copy to Clipboard Here our friend create() comes to the rescue again. In this case we are using it to create a new object and make it the value of Teacher.prototype. The new object has Person.prototype as its prototype and will therefore inherit, if and when needed, all the methods available on Person.prototype.
- We need to do one more thing before we move on. After adding the last line, the constructor property of Teacher.prototype is now equal to Person(), because we just set Teacher.prototype to reference an object that inherits its properties from Person.prototype! Try saving your code, loading the page in a browser, and entering Teacher.prototype.constructor into the console to verify.
- This can become a problem, so we need to set this right. You can do so by going back to your source code and adding the following line at the bottom:
Object.defineProperty(Teacher.prototype, 'constructor', {
value: Teacher,
enumerable: false, // so that it does not appear in 'for in' loop
writable: true });
- Now if you save and refresh, entering Teacher.prototype.constructor should return Teacher(), as desired, plus we are now inheriting from Person()!
因此,基于此,您的代码需要添加以下小部分:
Object.defineProperty(Cat.prototype, 'constructor', {
value: Cat,
enumerable: false, // so that it does not appear in 'for in' loop
writable: true });
请参阅下面的工作演示:
let log = console.log;
function Animal(name)
{
this.name = name;
}
Animal.prototype.walk = function(){
log(`${this.name} walks`);
}
function Cat(name)
{
Animal.call(this,name);
this.lives = 9;
}
Cat.prototype = Object.create(Animal.prototype);
Object.defineProperty(Cat.prototype, 'constructor', {
value: Cat,
enumerable: false, // so that it does not appear in 'for in' loop
writable: true });
Cat.prototype.meow = function(){
log("Meow!");
}
let bambi = new Cat("Bambi");
log(bambi.constructor);
但是,我建议只使用 ES6 classes 而不是手动执行此操作