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
阅读 1.7k
1 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题