Javascript对象工厂instanceof

时间:2016-03-08 17:13:58

标签: javascript instanceof objectfactory

为什么以下对象工厂模式代码返回false ...

var Animal = function(name) {
var temp = new Object();
    temp.name = name;
    return temp;
}

var animal = Animal("fox");

console.log(animal instanceof Animal);

...而以下构造函数模式代码返回true;

var Animal = function(name) {
    this.name = name;
}

var animal = new Animal("fox");

console.log(animal instanceof Animal);

0 个答案:

没有答案