In JavaScript Prototipo ereditarietà, qual è l'obiettivo dell'aggiunta della proprietà prototype.constructor. Lasciatemi spiegare con un esempio.Vantaggi dell'impostazione della proprietà "costruttore" nel "prototipo"
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();
Le righe seguenti restituiscono sempre true in ogni caso, quando si aggiunge Sub.prototype.constructor = Sub o no.
console.log(inst instanceof Sub) // true console.log(inst instanceof Super) // true
Suppongo che possa essere utile quando si ricevono nuove istanze ma quando e/o come?
Grazie in anticipo.
ottima domanda! – tybro0103