0

This is an example to demonstrate polymorphism example, at which if I remove following lines:

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

there is still no effect on the program and it gets the similar results. If so, how does this example demonstrates polymorphism? Commenting those lines, I see there are 2 functions which when call return results based on their own getInfo functions; so, where is the magic?

HTML:

<script type="text/javascript">
  function Person(age, weight) {
    this.age=age;
    this.weight=weight;
    this.getInfo=function() {
      return "I am " + this.age + " years old " +
        "and weighs " + this.weight +" kilo.";
    }
  }
  function Employee(age, weight, salary){
    this.salary=salary;
    this.age=age;
    this.weight=weight;
    this.getInfo=function() {
      return "I am " + this.age + " years old " +
        "and weighs " + this.weight +" kilo " +
        "and earns " + this.salary + " dollar.";
    }
  }
  Employee.prototype= new Person();
  Employee.prototype.constructor=Employee;
// The argument, 'obj', can be of any kind
// which method, getInfo(), to be executed depend on the object
// that 'obj' refer to.
  function showInfo(obj) {
    document.write(obj.getInfo()+"<br>");
  }
  var person = new Person(50,90);
  var employee = new Employee(43,80,50000);
  showInfo(person);
  showInfo(employee);
</script>

Result

enter image description here

Reference

http://w3processing.com/index.php?subMenuItemId=329

Deadpool
  • 7,811
  • 9
  • 44
  • 88

2 Answers2

1

This is really a weak example of polymorphism, besides the issue you have noticed (i.e. Employee.prototype setup is superflous, the prototype functions are never used in the example), here are a couple of other issues I see:

  1. The Employee function (i.e. the constructor) never calls the 'base class' constructor i.e. Person function - you would expect this to be in place given that they are in a parent-child relationship.
  2. As a consequence of the above issue, the Employee function has code copy-pasted from the Parent function:

    this.salary=salary; this.age=age; this.weight=weight;

  3. Idem for the getInfo function - both constructors create their specific functions and assign it to this; and the Employee version of getInfo never invokes the base class version - which it should maybe, to demonstrate inheritance

    So, really the only thing can be said in favor of this example is that it uses the same function name i.e. getInfo on the Person and Employee and invokes them on the two objects using common code. The can be termed as an example of polymorphism but probably an elementary one

Dhananjay
  • 544
  • 3
  • 7
  • It will be great if you can add a fiddle or provide a plausible example to learn polymorphism behavior in JS. Kindly, help out! – Deadpool Aug 25 '16 at 06:15
  • Please see [this post](http://stackoverflow.com/questions/39117168/how-to-properly-subclass-a-subclass-in-javascript) for a discussion of prototype based polymorphism. [This fiddle](https://jsfiddle.net/6x0agapd/) fixes the code discussed in the above post and is a working example of polymorphism. – Dhananjay Aug 25 '16 at 06:25
1

Inheriting "methods"

JavaScript does not have "methods" in the form that class-based languages define them. In JavaScript, any function can be added to an object in the form of a property. An inherited function acts just as any other property, including property shadowing as shown above (in this case, a form of method overriding).

When an inherited function is executed, the value of this points to the inheriting object, not to the prototype object where the function is an own property.

var o = {
      a: 2,
      m: function(b){
        return this.a + 1;
      }
    };

    console.log(o.m()); // 3
    // When calling o.m in this case, 'this' refers to o

    var p = Object.create(o);
    // p is an object that inherits from o

    p.a = 4; // creates an own property 'a' on p
    console.log(p.m()); // 5
    // when p.m is called, 'this' refers to p.
    // So when p inherits the function m of o, 
    // 'this.a' means p.a, the own property 'a' of p

you can find what you looking for in MDN LINK

Leon Barkan
  • 2,676
  • 2
  • 19
  • 43