AI编程助手
AI免费问答

javascript - js题,求解答

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
# JavaScript
PHPz PHPz 2966 天前 855 次浏览

全部回复(1) 我要回复

  • 天蓬老师

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

    如果构造函数返回了一个“对象”,那么这个对象会取代整个new出来的结果。如果构造函数没有返回对象,那么构造函数会默认返回this,也就是Dog.一般构造函数不返回值的。function Dog() { return new Animal};new Dog()等同于创建了 Animal 实例吧。不知道我分析的对不对,欢迎拍砖。

    回复
    0
  • 取消 回复 发送