JavaScriptプロトタイプ継承では、prototype.constructorプロパティを追加する目標は何ですか?例を挙げて説明しましょう。「プロトタイプ」内の「コンストラクタ」プロパティの設定の利点
var Super = function() { this.superProperty = 'Super Property' } var Sub = function() { this.subProperty = 'Sub Property' } Sub.prototype = new Super(); Sub.prototype.constructor = Sub; // advantages of the statement var inst = new Sub();
以下の行は、Sub.prototype.constructor = Subを追加すると常にtrueを返します。
console.log(inst instanceof Sub) // true console.log(inst instanceof Super) // true
私は、新しいインスタンスを取得するときに役立つかもしれませんが、いつ、どのようにするのですか?
ありがとうございます。
大きな質問です! – tybro0103