2016-11-14 15 views
2

私は暇な時間に少しJSを学ぼうとしていますが、私はこのテーマをテーマにしていました。私はperson.fullDetail();を実行傾ける理由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();

は誰も教えてもらえますか?

ご使用のバージョンのコードでご意見をお寄せいただければ、ありがとうございます。ありがとうございます。

+3

をあなたが定義する前にインスタンスを作成している、これを試してみてください機能! –

+0

@ DanielA.White Hoistingはこのうちのいくつかを修正しました – Feathercrown

+0

ホイストはここでのいくつかの機能にのみ適用されます。 – ssube

答えて

6

を役に立てば幸い

チェックのプロトタイプを作成するためのメソッドを追加した後に人物オブジェクトを作成しますプロトタイプを定義する前にオブジェクトを作成してください。

あなたはPerson現在の定義に基づいてオブジェクトを作っている

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

を行うと。その後、そのコードでは、あなたがこの問題を解決するために、ITの全体

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

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; 
 
}; 
 

 

 
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

はいこれは、人物オブジェクトを作成するときです。人物プロトタイプには、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());

は「それはあなたので

+0

これはこのユースケースを修正しますが、 'teacher'がまだ壊れているという事実は修正されません! – tcooc

+0

コンストラクタ関数(およびプロトタイプ変更)の前に人を作成することができる理由は、この厄介な/直感的な「ホイスト」https://developer.mozilla.org/のためですja/docs/Web/JavaScript /リファレンス/ステートメント/ var#var_hoisting。 – jlb

+0

@tcooc 'teacher'インスタンスを' Teacher'コンストラクタの下に移動しようとします – jlb

1

私はあなたのプロトタイプではまだ定義されてその機能を持たずに人と教師を作成するので、それはだと思います。参照してください

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

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

のようなあなたがES6である場合は、新しいクラス構文を使用することができ

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

関連する問題