Ich habe eine cat
, die von animal
erbt.JavaScript Basistyp ist `instanceof` abgeleiteten Typ Konstruktor
Ich würde erwarten, dass die cat
eine Instanz seines Konstruktors und der animal
Konstruktor sein.
Die animal
ist eine Instanz des cat
Konstruktors obwohl.
var animal = {};
var cat = Object.create(animal);
console.log('cat instanceof animal.constructor: ' + (cat instanceof animal.constructor));
console.log(' cat instanceof cat.constructor: ' + (cat instanceof cat.constructor));
console.log('animal instanceof cat.constructor: ' + (animal instanceof cat.constructor));
Warum ist die animal
eine Instanz des cat
?
Versuchen 'console.log (cat.constructor, cat.constructor === animal.constructor);' – 4castle
@ 4castle Danke, aber _why_ sind sie gleich? Ich verstehe nicht, wie mein abgeleiteter Typ und mein Basistyp denselben Konstruktor haben. – BanksySan
'cat' hat seine' constructor' Eigenschaft nicht definiert. Wenn Sie versuchen, auf 'cat.constructor' zuzugreifen, wird der Konstruktorwert des Prototyps verwendet. – 4castle