2016-12-12 5 views
4
var People = function() 
{ 
    this.FirstName = "John"; 
    this.LastName = "Doer"; 
} 

var Employee = function(o) 
{ 
    this.DateHired = "June 30"; 
    this.prototype = o; 
} 

var obj = new People(); 
var employee1 = new Employee(obj); 
print(employee1.DateHired); 
print(employee1.FirstName); 

フックアップ: 6月30日javascriptのプロトタイプpropertie出力

私は出力があるべき予想: 6月30日 Jonh

が、それは私が期待したものではありません。だから誰かが私に上記のコードに間違っていることを説明することができますか?

+1

どのようにあなたの 'プリント()' 'javacript'タグと関連?あなたの質問を修正してください – RomanPerekhrest

+0

これは 'console.log'でなければなりません。 – nmnsud

+0

可能な複製[Javascriptクラスを作成する正しい方法は何ですか?*](http://stackoverflow.com/questions/10769772/what -is-the-the-way-to-create-a-javascript-class) –

答えて

-3

あなたが言う:

this.prototype = o; 

あなたは、本質的に「プロトタイプ」という名前のプロパティを作成し、値を与えています。 Javascriptは、 "prototype"という名前の変数に値を格納しようとしていることを前提としています。

あなたが入力した場合:

print(employee1.prototype.FirstName); 

をあなたは

出力を得るでしょうが:ジョン

13

あなたはないnew Employeeによって作成インスタンス、上prototypeと呼ばれるプロパティを設定しましたプロトタイプチェーンを張るために何かをする。 prototypeプロパティはファンクションEmployeeになります。そのプロパティは、new Employeeで作成されたオブジェクトの内部プロパティを[[Prototype]]に設定するために使用されます。

だから、あなたはこのようにそれを行うだろう、あなたはPeopleのサブクラスであることをEmployeeをしたいと仮定すると:しかし

// The singular is "Person", so I'll use that instead of People 
 
var Person = function() { 
 
    this.FirstName = "John"; 
 
    this.LastName = "Doer"; 
 
}; 
 

 
var Employee = function() { 
 
    // Give `Person` a chance to do its initialization of the instance 
 
    Person.call(this/*, possible, arguments, here*/); 
 

 
    // Now carry on with the `Employee` initialization 
 
    this.DateHired = "June 30"; 
 
}; 
 

 
// Make Employee a subclass of Person by 
 
// 1. Giving `Employee.prototype` a new object whose [[Prototype]] is 
 
// `Person.prototype` 
 
// 2. Ensuring that `Employee.prototype`'s `constructor` property points 
 
// back to Employee. 
 
Employee.prototype = Object.create(Person.prototype); 
 
Employee.prototype.constructor = Employee; 
 

 
// Usage 
 
var employee1 = new Employee(); 
 
console.log(employee1.DateHired); 
 
console.log(employee1.FirstName);

、ここでは、我々ドン2016年の終わりにこれらのゲームをこれ以上実行する必要はありません。

(私たちは古いブラウザにコードをデプロイする必要がある場合、換気する)ES2015の class構文を使用できます。

class Person { 
 
    constructor() { 
 
    this.FirstName = "John"; 
 
    this.LastName = "Doer"; 
 
    } 
 
} 
 

 
class Employee extends Person { 
 
    constructor() { 
 
    // Give `Person` its chance to initialize the instance 
 
    super(); 
 
    // Now carry on with the `Employee` initialization 
 
    this.DateHired = "June 30"; 
 
    } 
 
} 
 

 
// Usage 
 
const employee1 = new Employee(); 
 
console.log(employee1.DateHired); 
 
console.log(employee1.FirstName);

+0

また、 'People.apply(this、arguments);'を追加することも可能です。従業員の機能の始めに。 –

+0

@AliaksiejMaroz:はい、すべての引数を渡す必要がある場合のみです。通常は選択して選択します。 –

+0

うん、そうだよ。私の例は、この特別な場合にのみ良いでしょう。 –

0
Inheritance works as below 


    var People = function() 
      { 
       this.FirstName = "John"; 
       this.LastName = "Doer"; 
      } 

      var Employee = function() 
      { 
       this.DateHired = "June 30"; 
       //this.prototype = o; 
       People.call(this); 
      } 
      Employee.prototype=Object.create(People.prototype); 
      var employee1 = new Employee(); 
      console.log(employee1.DateHired); 
      console.log(employee1.FirstName); 
+0

コメントをお願いします。 – pareshm

+0

主な問題は、* JavaScriptで継承がどのように機能するかではないということです。そのコードには継承はありません。オブジェクトにプロパティを設定する関数があります。 'obj'変数にはまったく目的がありません。あなたは' Employee'で 'o'引数を使用しません。 –

+0

@ T.J.Crowder:今はいいですか? – pareshm

関連する問題