2016-11-14 81 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());
(͡° ͜ʖ ͡°)