我试图更好地理解 Javascript 中的 OOP。有人可以解释在下面的示例中如何使用 create 方法和替代方法吗?我在网上查过它并查看了关于 SO 的几篇文章,但仍然没有牢牢掌握下面代码中发生的事情。我提供了评论来说明我的理解。请纠正我哪里错了。
此示例用于覆盖基类中的方法:
// Defines an Employee Class
function Employee() {}
// Adds a PayEmployee method to Employee
Employee.prototype.PayEmployee = function() {
alert('Hi there!');
}
// Defines a Consultant Class and
// Invokes the Employee Class and assigns Consultant to 'this' -- not sure and not sure why
// I believe this is a way to inherit from Employee?
function Consultant() {
Employee.call(this);
}
// Assigns the Consultant Class its own Constructor for future use -- not sure
Consultant.prototype.constructor = Consultant.create;
// Overrides the PayEmployee method for future use of Consultant Class
Consultant.prototype.PayEmployee = function() {
alert('Pay Consultant');
}