简体   繁体   English

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

[英]Creating multiple instances of derived class in javascript

Can someone tell the difference between how the following set of code is getting executed in depth. 有人可以告诉我们下面的代码如何被深入执行之间的区别。

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

My doubt is how should i change my code such that for every instance of Employee class i will have a different name. 我的疑问是我应该如何更改代码,以便对于Employee类的每个实例我都有不同的名称。

Edit: 编辑:

I know that i should call Person constructor but my doubt is. 我知道我应该打电话给Person构造函数,但我的疑问是。 Let me put the two methods of code. 让我介绍两种代码方法。

Method 1: 方法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");

Fig 1: 图。1:

在此处输入图片说明

Method 2: 方法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");

Fig 2: 图2: 在此处输入图片说明

My doubt is in Method 1 we don't have name property because we have not called Person constructor. 我的疑问是方法1中没有name属性,因为我们没有调用Person构造函数。 But in Method 2 we have name property because we called Person constructor. 但是在方法2中,我们具有name属性,因为我们调用了Person构造函数。 But why name property is assigned to Employee object instead of Person. 但是,为什么将name属性分配给Employee对象而不是Person。

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

Firstly, your person object hard-codes your name, change the constructor to accept names: 首先,您的person对象对您的姓名进行硬编码,将构造函数更改为接受姓名:

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

Now for the tricky part, in the Employee constructor, you need to call the "super" class - the class it inherits from. 现在,对于棘手的部分,在Employee构造函数中,您需要调用“ super”类-它继承自该类。 this is a bit weird in javascript: 这在javascript中有点奇怪:

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

Creating anew Employee would now be done like this: 现在可以像下面这样创建新员工:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM