在javascript中创建派生类的多个实例

时间:2015-03-25 18:52:16

标签: javascript inheritance prototype prototypal-inheritance

有人可以区分下面的代码集如何深入执行。

function Person(){
this.name = "Jagadish";
}

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

function Employee(designation){
   this. designation = designation;
}

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

Employee.prototype.getDesignation = function(){
   return this.designation;
}

var employee1= new Employee("Cons");
console.log(employee1.getName()+ " --- "+ employee1.designation); 
// Jagadish --- Cons

var employee2= new Employee("Dev");
console.log(employee2.getName()+ " --- "+ employee2.designation);
// Jagadish --- Dev

我的疑问是我应该如何更改我的代码,以便对于Employee类的每个实例,我将使用不同的名称。

修改

我知道我应该调用Person构造函数,但我的疑问是。让我把两种代码方法放在一起。

方法1:

function Person(name){
    this.name = name;
}

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

function Employee(name, designation){
    //Person.call(this, name);
    this. designation = designation;
}

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

Employee.prototype.getDesignation = function(){
    return this.designation;
}

new Employee("Jagadish", "Cons");

图1:

enter image description here

方法2:

function Person(name){
    this.name = name;
}

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

function Employee(name, designation){
    Person.call(this, name);
    this. designation = designation;
}

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

Employee.prototype.getDesignation = function(){
    return this.designation;
}

new Employee("Jagadish", "Cons");

图2: enter image description here

我怀疑在方法1中我们没有name属性,因为我们没有调用Person构造函数。但是在方法2中我们有name属性,因为我们调用了Person构造函数。但是为什么name属性被分配给Employee对象而不是Person。

2 个答案:

答案 0 :(得分:1)

function Person(name){
  this.name = name; // Update your Person class to accept a name
}

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

function Employee(name, designation){
   this. designation = designation;
   Person.call(this, name); // Update Employee to accept a name and call super
}

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

Employee.prototype.getDesignation = function(){
   return this.designation;
}

var employee1= new Employee("Sue", "Cons");
console.log(employee1.getName()+ " --- "+ employee1.designation); 
// Sue --- Cons

var employee2= new Employee("Bob", "Dev");
console.log(employee2.getName()+ " --- "+ employee2.designation);
// Bob --- Dev

答案 1 :(得分:1)

首先,你的person对象硬编码你的名字,改变构造函数接受名字:

function Person(name){
    this.name = name;
}

现在,对于棘手的部分,在Employee构造函数中,您需要调用" super" class - 它继承的类。这在javascript中有点奇怪:

function Employee(name, designation){
   Person.call(this, name); // call Person constructor to set name
   this.designation = designation;
}

现在创建一个新的员工就像这样:

employee1 = new Employee('John', 'Manager')