batman
batman

Reputation: 5370

How to inherit base class properties and call base class constructor in javascript?

Let's say I have an Employee class:

function Employee(name, age, salary) {
  this.name = name;
  this.age = age;
  this.salary = salary;
}

function Manager(name, age, salary, management_school_name) {
  ...
}

Manager.prototype = new Employee();
Manager.prototype.constructor = Manager;

In the above code, I want to make use of the fact that Employee encapsulates name, age and salary.

So how should I go about dealing with repeated parameters?

Upvotes: 2

Views: 461

Answers (5)

Niranjan Borawake
Niranjan Borawake

Reputation: 1638

It is properly explained in above posts WHAT you wanted to achieve. But AFAIK below lines will explaing WHY it is so.

There are two ways you can add public properties and methods to your class (rather function class) :

Method 1 of adding public property, added to each instance:

function MyClass(){
      this.publicProperty = 'public property'; 
}

Method 2 of adding public property, added to prototype, common for all instances:

MyClass.prototype.publicMethod = function(){
      console.log('public method');
}

When you want to inherit from a Class you need to inherit all the public properties and methods.

Inheriting properties and methods added using method 1:

function MyDerivedClass(){
      MyClass.apply(this);
}

Inheriting properties and methods added using method 2:

MyDerivedClass.prototype = Object.create(MyClass.prototype);

Hope this helps.

Upvotes: 0

HMR
HMR

Reputation: 39260

As stated before; it's best not to create an instance of Parent to set at the prototype of Child. To re use Parent constructor you can do Parent.call(this,args); in Child.

I like to use an arguments object for better readability and when a bunch of functions are called in a chain the functions can take out and mutate the part that concerns them. For example

function Employee(args) {
  //name is mandatory
  if(typeof args.name === "undefined")
    throw new Error("Cannot create an Employee instance without passing a name.");
  this.name = args.name;
  //age is optional and default is 25
  this.age = (typeof args.age === "undefined")? 25:args,age;
  //optional, defaults to 2500
  this.salary = (typeof args.salary === "undefined")?2500:args.salary;
}

function Manager(args) {
  //re use Employee constructor
  Employee.call(this,args);
  //set Employee specific values same as Employee
}
Manager.prototype = Object.create(Employee.prototype);
Manager.prototype.constructor = Manager;

var m = new Manager({name:"Harry",age:33,salary:5000});

More info about constructor functions and prototype here.

Upvotes: 1

Khanh TO
Khanh TO

Reputation: 48972

function Employee(name, age, salary) {
  this.name = name;
  this.age = age;
  this.salary = salary;
}

function Manager(name, age, salary, management_school_name) {
  Employee.call(this,name,age,salary); //Call base constructor function
...
}
Manager.prototype = new Employee(); //Create prototype chain
Manager.prototype.constructor = Manager;

Another way to create prototype chain is using Object.create.

Manager.prototype = Object.create(Employee.prototype); //Create prototype chain

This is how Object.create is implemented internally:

function create(proto) {
  function F() {};      
  F.prototype = proto ;
  return new F();      
}

So when should we use Object.create and new Employee() to create prototype chain?

Object.create does not have any construction logic to create an object while we could have construction logic inside Employee, like this.default = "default". In this case, using new Employee() is not much different from Object.create. But if we need to avoid construction logic completely, we could use Object.create

Upvotes: 4

Ifch0o1
Ifch0o1

Reputation: 924

You can try this:

Function.prototype.inherit = function(Parent) {
    this.prototype = Object.create(Parent.prototype)
    this.prototype.constructor = this
}

Source (Bulgarian Lang)

You can see also this demo

Upvotes: 1

Ringo
Ringo

Reputation: 3965

Manager is an staff too, so why don't you do this:

function Staff(name, age, salary) {
  this.name = name;
  this.age = age;
  this.salary = salary;
}

function Position(positionName, management_school_name) {
   this.positionName = positionName;
   this.managent_school_name = management_shool_name;
}

Staff.prototype.position = new Position();

Upvotes: 0

Related Questions