我试图在Javascript中更好地理解OOP。有人可以解释下面的例子中如何使用create方法和替代方法吗?我在网上查了一下,查看了几篇关于SO的文章,但仍然没有掌握下面代码中发生的事情。我提供了评论来说明我的理解。请纠正我错在哪里。了解Javascript构造函数:自定义创建方法
这个例子是用来覆盖从基类的方法:
// 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');
}
老板,哪里还有我的薪水是多少? 〜''你好!':'P' –
'Object.create'在该代码中根本没有被使用。 –
和'Consultant.create'不存在。 @ T.J .:你解释了这一千次,你肯定找到最好的重复;) –