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
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.