0

I have created two constructor Person and Employee. Employee construct inheriting the property of Person. This working fine. Now I want to add new property in Person constructor with prototype. But getting blank string. Please help.

JS Code

function Person (age, weight) {
        this.age = age;
        this.weight = weight;
    }

    Person.prototype.name = name;

    //we will give Person the ability to share their information.
    Person.prototype.getInfo = function () {
        return "I am " + this.age + " year old and weight " + this.weight + " kg.";
    };


    function Employee (age, weight, salary, name) {

        //Person.call(this, age, weight);  // by call parameters as arguments
        Person.apply(this, arguments);  // by apply arguments as array
        this.salary = salary;
    }

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

    Employee.prototype.getInfo = function () {
        return "I am "+this.name+" " + this.age + " year old and weight " + this.weight + " kg having $" + this.salary +" pm salary";
    }


    var person = new Employee(30, 70, 4000, "Manish");
    console.log(person.getInfo());

    document.write(person.getInfo());

Demo Fiddle

Rakesh Kumar
  • 2,705
  • 1
  • 19
  • 33

2 Answers2

0

You could try this implementation:

function Person (age, weight, name) {
    var  proto = this.getProto(this);
    this.age = age;
    this.weight = weight;
    if (proto) {
        proto.name = name;
    }
}

Person.prototype.getProto = function (instance) {
    var proto;
    if (Object.getPrototypeOf) {
        proto = Object.getPrototypeOf(instance);
    } else {
        proto = instance.__proto__;
    }
    return proto;
}

//we will give Person the ability to share their information.
Person.prototype.getInfo = function () {
    return "I am " + this.age + " year old and weight " + this.weight + " kg.";
};


function Employee (age, weight, salary, name) {

    Person.call(this, age, weight, name);  // by call parameters as arguments
    //Person.apply(this, arguments);  // by apply arguments as array
    this.salary = salary;
}

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

Employee.prototype.getInfo = function () {
    return "I am "+this.name+" " + this.age + " year old and weight " +
        this.weight + " kg having $" + this.salary +" pm salary";
}


var person = new Employee(30, 70, 4000, "Manish");
console.log(person.getInfo());

document.write(person.getInfo());

Also better using Object.create for inheritance instead of instance more details about that please read in answer

More different approached for getting prototype in answer

Community
  • 1
  • 1
Pencroff
  • 1,009
  • 9
  • 13
0

I have used below approach to accomplish my requirement with Object.create as suggested by Pencroff.

function Person () {};
function Employee(){};

// helper function for Object.create()
Function.prototype.extends = function(superclass){
    this.prototype = Object.create(superclass.prototype);
    this.prototype.constructor = this;
}

Employee.extends(Person);

//Helper function for add methods in consturctor.
Function.prototype.methods = function(name, func){
    this.prototype[name] = func;
    return this;
}

Person
    .methods("name", function(name){ return name; })
    .methods("age", function(age){ return age;})
    .methods("weight", function(weight){ return weight;})
    .methods("getPersInfo", function(age, weight){
        var persAge = this.age(age),
            persWeight = this.weight(weight);
        return "I am " + persAge + " year old and weight " + persWeight + " kg.";
    });

Employee
    .methods("salary", function(salary){return salary;})
    .methods("getInfo", function(name, age, weight, salary){
        var empAge = this.age(age),
            empWeight = this.weight(weight),
            empName = this.salary(name),
            empSalary = this.salary(salary);

        return "Empoyee name is "+empName+ " age " + empAge + " year old and weight " + empWeight + "kg. having $"+empSalary+" in hand.";
    });



var person = new Person();
var employee = new Employee();

console.log(employee.getInfo("Mahesh", 30, 70, 4000));
document.write(employee.getInfo("Mahesh", 30, 70, 4000));
Rakesh Kumar
  • 2,705
  • 1
  • 19
  • 33