search

Home  >  Q&A  >  body text

javascript - js question, please answer

function Animal() {};
function Cat() {};
function Dog() { return new Animal};
Cat.prototype = new Animal;
console.log(new Dog instanceof Animal);//true为什么?
console.log(new Dog instanceof Dog);//false 为什么?
console.log(new Cat instanceof Animal);//true
PHPzPHPz2835 days ago674

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-05-19 10:36:49

    If the constructor returns an "object", then this object will replace the entire new result. If the constructor does not return an object, the constructor will return this by default, which is the Dog.一般构造函数不返回值的。function Dog() { return new Animal};new Dog()等同于创建了 Animal instance. I don’t know if my analysis is correct or not. Comments are welcome.

    reply
    0
  • Cancelreply