比较---原型---> Animal.prototype(constructor,run)
兔子--- [[prototype]] --->比较
兔子---原型--->动物
Rabbit.prototype --- [[[prototype]] ---> Animal.prototype(constructor,run)
rabbit(name:'White Rabbit')--- [[[prototype]] ---> Rabbit.prototype
兔子(名字:'白兔子')--- prototype ---> Rabbit
。
认为他们是真的。但是,请指定是否有错误。我写了一些代码来理解“继承”的主题。但是有几个没有给我想要的结果。但是有几个没有给我想要的结果。(注释行中的规范)
class Animal {
constructor(name, speed) {
this.speed = speed;
this.name = name;
}
run(speed = 0) {
this.speed += speed;
console.log(`${this.name} runs with speed ${this.speed}.`);
}
static compare(animalA, animalB) {
console.log(animalA.speed - animalB.speed);
}
}
class Rabbit extends Animal {
hide() {
console.log(`${this.name} hides!`);
}
}
let rabbits = [
new Rabbit("White Rabbit", 5),
new Rabbit("Black Rabbit", 10)
];
console.log(Rabbit.__proto__ === Animal); // true (not problem)
console.log(Animal.__proto__ === Function.prototype); // true (not problem)
console.log(Rabbit.__proto__ === Animal.prototype); //(not problem)
console.log(Rabbit.__proto__.prototype === Animal.prototype); //(not problem)
console.log(rabbits[1].__proto__ === Animal.prototype);
// this problem
// rabbit(name:'White Rabbit') ---[[prototype]]---> Rabbit.prototype ?
慕田峪9158850
慕码人8056858
相关分类