Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...