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

batman

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?

Khanh TO
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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

can we inherit base class constructor in java

From Dev

How to inherit a python base class?

From Dev

Base Constructor Call in Derived Class

From Dev

How to call constructor of a template base class in a template derived class?

From Dev

Inherit XMLType of base class

From Dev

How to call "base class" function with Javascript (jQuery)?

From Dev

Accesing constructor's base class properties

From Dev

How to inherit Base class with singleton in python

From Dev

Call constructor from "abstract" base "class"

From Dev

Is it legal to explicitly call base class destructor/constructor?

From Dev

Why I cannot call the base class Constructor?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

inherit from a class constructor javascript

From Dev

inherit from a class constructor javascript

From Java

Why doesn't a class having private constructor prevent inheriting from this class? How to control which classes can inherit from a certain base?

From Dev

Plugin and base class constructor

From Dev

Constructor in base and derived class

From Dev

How do I call a derived class method from the base class constructor in C++?

From Dev

How to instantiate inherited class using base constructor?

From Dev

How to use a copy constructor with a base class?

From Dev

How to break at a constructor of base class using gdb?

From Dev

Publicly inherit from base class, privately inherit from derived class

From Dev

Qt - Does a derived class inherit its base class's meta properties?

From Dev

How to inherit DatabaseContext as a base class across multiple injected services

From Dev

How to move similar properties to base class

From Dev

Call base member in lambda function from inherited class constructor?

From Dev

Can't call base class constructor with brace initialization intellisense error

From Dev

Call base class constructor without manually supplying arguments

Related Related

  1. 1

    can we inherit base class constructor in java

  2. 2

    How to inherit a python base class?

  3. 3

    Base Constructor Call in Derived Class

  4. 4

    How to call constructor of a template base class in a template derived class?

  5. 5

    Inherit XMLType of base class

  6. 6

    How to call "base class" function with Javascript (jQuery)?

  7. 7

    Accesing constructor's base class properties

  8. 8

    How to inherit Base class with singleton in python

  9. 9

    Call constructor from "abstract" base "class"

  10. 10

    Is it legal to explicitly call base class destructor/constructor?

  11. 11

    Why I cannot call the base class Constructor?

  12. 12

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  13. 13

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  14. 14

    inherit from a class constructor javascript

  15. 15

    inherit from a class constructor javascript

  16. 16

    Why doesn't a class having private constructor prevent inheriting from this class? How to control which classes can inherit from a certain base?

  17. 17

    Plugin and base class constructor

  18. 18

    Constructor in base and derived class

  19. 19

    How do I call a derived class method from the base class constructor in C++?

  20. 20

    How to instantiate inherited class using base constructor?

  21. 21

    How to use a copy constructor with a base class?

  22. 22

    How to break at a constructor of base class using gdb?

  23. 23

    Publicly inherit from base class, privately inherit from derived class

  24. 24

    Qt - Does a derived class inherit its base class's meta properties?

  25. 25

    How to inherit DatabaseContext as a base class across multiple injected services

  26. 26

    How to move similar properties to base class

  27. 27

    Call base member in lambda function from inherited class constructor?

  28. 28

    Can't call base class constructor with brace initialization intellisense error

  29. 29

    Call base class constructor without manually supplying arguments

HotTag

Archive