ClassAのプロパティをClassBに「コピー」または「実装」するいくつかの大規模なプロジェクトでこのJavaScriptパターンを確認しましたが、「。call()」を使用してClassBのコンストラクター内でClassAの空のコンストラクターを呼び出す目的を理解できませんでした。 「this」バインディングとしてのClassBインスタンス?
var ClassA = function() {
//this function is totally empty
};
ClassA.prototype = {
jump: function() {...some code...},
dance: function() {
if (this.canDance) {
alert("dance");
}
}
};
ClassA.implementOn = function(targetClassOrObject) {
for ( var prop in ClassA.prototype ) {
targetClassOrObject[ prop ] = ClassA.prototype[ prop ];
}
};
var ClassB = function() {
this.canDance = true;
ClassA.call(this); // <------------What's the purpose of this line?
};
ClassA.implementOn(ClassB.prototype);
var instanceOfB = new ClassB();
instanceOfB.dance();