Javascript で OOP をよりよく理解しようとしています。以下の例と別の方法で create メソッドがどのように使用されているか説明してもらえますか? 私はそれを Web で調べ、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');
}