JS OOP: 11. Prototype inheritance benar
by istiq20
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Prototype inheritance benar</title>
</head>
<body>
<script>
function Employee(firstName) {
this.firstName = firstName
}
function Manager(firstName) {
this.firstName = firstName;
}
// Manager.prototype = Employee.prototype; // salah
Manager.prototype = Object.create(Employee.prototype);
Manager.prototype.sayHello = function(name){
console.info(`Hello ${name}, my name is Manager ${this.firstName}`);
}
Employee.prototype.sayHello = function(name){
console.info(`Hello ${name}, my name is Employee ${this.firstName}`);
}
const employee = new Employee("Eko");
employee.sayHello("Joko"); // Hello Joko, my name is Employee Eko
const manager = new Manager("Budi");
manager.sayHello("Joko"); // Hello Joko, my name is Manager Budi
console.info(employee);
console.info(manager);
</script>
</body>
</html>