Heim > Fragen und Antworten > Hauptteil
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
天蓬老师2017-05-19 10:36:49
如果构造函数返回了一个“对象”,那么这个对象会取代整个new出来的结果。如果构造函数没有返回对象,那么构造函数会默认返回this,也就是Dog
.一般构造函数不返回值的。function Dog() { return new Animal};new Dog()
等同于创建了 Animal
实例吧。不知道我分析的对不对,欢迎拍砖。