-
Notifications
You must be signed in to change notification settings - Fork 0
polymorphism
Jorge Estanislao Barsoba edited this page Mar 24, 2022
·
1 revision
Polymorphism is the practice of designing objects to share behaviors, and to be able to override shared behaviors with specific ones. It takes advantage of inheritance in order to make this happen.
function Person(age, weight) {
this.age = age;
this.weight = weight;
}
Person.prototype.getInfo = function () {
return (
"I am " + this.age + " years old " + "and weighs " + this.weight + " kilo."
);
};
function Employee(age, weight, salary) {
this.age = age;
this.weight = weight;
this.salary = salary;
}
Employee.prototype = new Person();
Employee.prototype.getInfo = function () {
return (
"I am " +
this.age +
" years old " +
"and weighs " +
this.weight +
" kilo " +
"and earns " +
this.salary +
" dollar."
);
};
var person = new Person(50, 90);
var employee = new Employee(43, 80, 50000);
console.log(person.getInfo());
console.log(employee.getInfo());Sources