JS原型和继承

Vla*_*lad 2 javascript prototype prototypal-inheritance

在业余时间,我尝试学习一点JS,但我坚持主题的主题.

var person = new Person("Bob", "Smith", 52);
var teacher = new Teacher("Adam", "Greff", 209);

function Humans(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

function Person(firstName, lastName, age) {
  Humans.call(this, firstName, lastName);
  this.age = age;
}

Person.prototype = Object.create(Humans.prototype);

Person.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.age;
};


function Teacher(firstName, lastName, roomNumber) {
  Humans.call(this, firstName, lastName);
  this.room = roomNumber;
}

Teacher.prototype = Object.create(Humans.prototype);

Teacher.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.room;
};

person.fullDetail();
Run Code Online (Sandbox Code Playgroud)

谁能告诉我为什么我不能执行person.fullDetail();

如果你能用你的代码版本做一些评论,我将非常感激,谢谢.

Mik*_*uck 6

因为您在定义原型应该是什么之前创建对象.

当你这样做

var person = new Person ("Bob", "Smith", 52);
Run Code Online (Sandbox Code Playgroud)

你是根据当前的定义制作一个对象的Person.后来在该代码,你改变的雏形Person,在它的全部

Person.prototype = Object.create(Humans.prototype);
Run Code Online (Sandbox Code Playgroud)

要解决此问题,请在完成原型重新分配创建对象.

function Humans(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

function Person(firstName, lastName, age) {
  Humans.call(this, firstName, lastName);
  this.age = age;
}

Person.prototype = Object.create(Humans.prototype);

Person.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.age;
};


function Teacher(firstName, lastName, roomNumber) {
  Humans.call(this, firstName, lastName);
  this.room = roomNumber;
}

Teacher.prototype = Object.create(Humans.prototype);

Teacher.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.room;
};

var person = new Person("Bob", "Smith", 52);
var teacher = new Teacher("Adam", "Greff", 209);
console.log(person.fullDetail());
Run Code Online (Sandbox Code Playgroud)

  • 你可以通过显示`Object.getPrototypeOf(person)`!==`Person.prototype`来证明它.原型或原件是人员实例被"丢失"并被替换.javascript继承非常有趣的边缘情况! (3认同)

Gee*_*eky 5

是的,因为当你创建人物对象时,人物原型没有FullDetail方法.

更改对象创建的顺序,在将方法添加到原型后创建人物对象

检查这个片段

var teacher;
var person;
function Humans(firstName, lastName) {
  this.firstName = firstName;
  this.lastName = lastName;
}

function Person(firstName, lastName, age) {
  Humans.call(this, firstName, lastName);
  this.age = age;
}

Person.prototype = Object.create(Humans.prototype);

Person.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.age;
};

 person = new Person("Bob", "Smith", 52);

function Teacher(firstName, lastName, roomNumber) {
  Humans.call(this, firstName, lastName);
  this.room = roomNumber;
}

Teacher.prototype = Object.create(Humans.prototype);

Teacher.prototype.fullDetail = function() {
  return this.firstName + " " + this.lastName + " " + this.room;
};
teacher= new Teacher("Adam", "Greff", 209);
console.log(person.fullDetail());
console.log(teacher.fullDetail());
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你