2016-11-14 78 views
2

在我的业余时间,我尝试学习一点JS,但我坚持主题的主题。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();

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

+3

你定义之前创建的实例功能! –

+0

@ DanielA.White起重修复了部分此类 – Feathercrown

+0

起重仅适用于此处的一些功能。 – ssube

回答

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

+3

你可以通过显示Object.getPrototypeOf(person)'!== Person.prototype'来证明它。原型或原始人物实例“丢失”并被替换。 JavaScript继承非常有趣的边缘情况! – tcooc

5

是的,这是因为当你创建person对象Person原型没有FullDetail方法。

更改你的对象创建的排序,创建Person对象添加方法后以原形

检查这个片段

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

希望它可以帮助

+0

虽然这修复了这个用例,但它并没有解决'老师'仍然坏的事实! – tcooc

+0

只是要补充一点,你能够在构造函数(和原型变化)之前创建一个人的原因是因为这种令人讨厌/反直觉的事情,叫做'吊起'https://developer.mozilla.org/ EN /文档/网络/的JavaScript /参考/语句/ VAR#var_hoisting。 – jlb

+0

@tcooc尝试将'teacher'实例化下移到'Teacher'构造函数下面 – jlb

1

我想这是因为你无需在原型尚未定义它们的功能创建者和教师。试试这个:

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());
(͡° ͜ʖ ͡°)