2016-07-01 16 views
0
<script> 
    var Employee = new function(name) 
    { 
    this.name=name; 
    } 
    Employee.prototype.getName = function() 
    { 
     return this.name; 
    } 

    var PermanenetEmployee = new function(annualsalary) 
    { 
    this.annualsalary=annualsalary; 
    } 
    var employee = new Employee("rahul"); 
    PermanenetEmployee.prototype = employee; 
    var pe = new PermanenetEmployee(5001); 
    document.write(pe.getName()); 



    </script> 

私はJavaスクリプトで継承を実装しています。このコードから私は "rahul"のような従業員の名前を印刷したいと思いますが、Uncaughtのようなエラーが発生します。TypeError:未定義(匿名関数)のプロパティ 'getName'を設定できません。Uncaught TypeError:未定義(無名関数)のプロパティ 'getName'を設定できません。

Employee.prototype.getName = function() 
     { 
      return this.name; 
     } 

答えて

2

これが問題である:(。そしてPermanenetEmployeeも同じ)

var Employee = new function(name) 
// ------------^^^ 
{ 
this.name=name; 
} 

あなたはそこnewを望んでいません。 newは、関数を呼び出します。後でemployeeに割り当てるときと同じようにしたいとします。


これらの間の継承を設定する方法は、アンチパターンです。 PermanenetEmployeeが正しく "サブクラス" Employeeようにするには、この操作を行います。

PermanenetEmployee.prototype = Object.create(Employee.prototype); 
PermanenetEmployee.prototype.constructor = PermanenetEmployee; 

ない

var employee = new Employee("rahul"); 
PermanenetEmployee.prototype = employee; 

を...そしてPermanenetEmployeenameを受け入れ、Employeeに渡しています

var PermanenetEmployee = function(name, annualsalary) { 
    Employee.all(this, name); // <==== 
    // ... 
}; 

。 ..またはより良い使用は、ES2015( "ES6")class(あなたが必要な場合、例えばBabelと一緒に蒸散)を使用してください。

ここで正しい設定です。あなたがいる場合transpileする必要があることに注意し、再び

class Employee { 
 
    constructor(name) { 
 
     this.name = name; 
 
    } 
 
    getName() { 
 
     return this.name; 
 
    } 
 
} 
 

 
class PermanentEmployee extends Employee { 
 
    constructor(name, annualSalary) { 
 
     super(name); 
 
     this.annualSalary = annualSalary; 
 
    } 
 

 
    getAnnualSalary() { 
 
     return this.annualSalary; 
 
    } 
 
} 
 

 
// Using 
 
var pe = new PermanentEmployee("Rahul", 5001); 
 
console.log(pe.getName()); 
 
console.log(pe.getAnnualSalary());

var Employee = function(name) { 
 
    this.name = name; 
 
}; 
 
Employee.prototype.getName = function() { 
 
    return this.name; 
 
}; 
 

 
var PermanentEmployee = function(name, annualSalary) { 
 
    Employee.call(this, name); 
 
    this.annualSalary = annualSalary; 
 
}; 
 

 
// Set up subclass 
 
PermanentEmployee.prototype = Object.create(Employee.prototype); 
 
PermanentEmployee.prototype.constructor = PermanentEmployee.prototype; 
 

 
PermanentEmployee.prototype.getAnnualSalary = function() { 
 
    return this.annualSalary; 
 
}; 
 

 
// Using 
 
var pe = new PermanentEmployee("Rahul", 5001); 
 
console.log(pe.getName()); 
 
console.log(pe.getAnnualSalary());

とES2015を持つ:私はまたPermanenetEmployeeのタイプミスを修正しましたその構文を野生で使用したい(今のところ)。

0

JSで継承される方法はいくつかありますが、私はこのパターンを使用しています。

PermanentEmployee = function() { 
    Employee.call(this); 
}; 

PermanentEmployee.prototype = Object.create(Employee.prototype); 
PermanentEmployee.constructor = PermanentEmployee; 

PermanentEmployee.prototype.foo = function() {} 
:ベースを継承し

Employee = function() { 
}; 
Employee.prototype = { 
    getName: function() {} 
}; 

そしてプロトタイプ:

まずベースのプロトタイプを宣言

関連する問題