为什么所有__proto__对象都具有相同的名称?

时间:2019-06-26 02:33:49

标签: javascript inheritance prototype

为什么 proto 在浏览器中具有相同的名称?当我在浏览器中检查代码结果时,继承工作按预期进行,但是当您在控制台中检查python对象时, proto 对象都具有相同的名称Animal。

function Animal(name) {
  this.name = name;
}

Animal.prototype.walk = () => {
  console.log(`${this.name} is walking`);
};

function Snake(name, specialMove) {
  this.specialMove = specialMove;
  Animal.call(this, name);
}

Snake.prototype = Object.create(Animal.prototype);
Snake.prototype.hiss = () => {
  console.log(`${this.name} special move ${this.specialMove}`);
};

function Python(name, specialMove) {
  Snake.call(this, name, specialMove);
}
Python.prototype = Object.create(Snake.prototype);
Python.prototype.Strangle = () => console.log('Strangle');

const animal = new Animal('Gary');
console.log(animal);
animal.walk();

const snake = new Snake('Franky', 'Ragan Cajan bite attack');
console.log(snake);
snake.hiss();

const python = new Python('The King', 'The signiture');
console.log(python);

0 个答案:

没有答案