JS原型和继承

时间:2016-11-14 21:16:12

标签: 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();

有人可以告诉我为什么我不能执行person.fullDetail();吗?

如果您可以对您的代码版本发表一些评论,我将非常感激,谢谢。

4 个答案:

答案 0 :(得分:6)

因为你在定义他们的原型应该是什么之前创建你的对象。

当你这样做时

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

您正在根据Person当前定义创建对象。稍后在该代码中,您将完整地更改Person的原型

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

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

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());

答案 1 :(得分: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());




希望有所帮助

答案 2 :(得分:1)

我认为这是因为你创建的人和老师没有在原型中定义他们的功能。试试这个:

$connectionInfo = array( "Database"=>"dbName");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
    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());

答案 3 :(得分:0)

如果你在ES6,你可以使用新的类语法,如

class ExampleBaseClass {
    // Do things here...
}

class ExampleClass extends ExampleBaseClass {
    // Do other things here...
    // You may use methods from ExampleBaseClass here.
}

请参阅:https://www.sitepoint.com/understanding-ecmascript-6-class-inheritance/