为什么instanceof返回false?

时间:2018-11-30 09:11:01

标签: javascript null instanceof

这是我的代码

function Otaku (name, age) {

  this.name = name;
  this.age = age;

  this.habit = 'Games';
}

Otaku.prototype.strength = 60;
Otaku.prototype.sayYourName = function () {
  console.log('I am ' + this.name);
}


function newTest() {

  const obj = {}
  const ctu = [].shift.call(arguments)
  obj.__proto__ = ctu.prototype
  ctu.apply(obj, arguments)
  return obj
}
function objectFactory() {

  const obj = Object.create(null)
  Constructor = [].shift.call(arguments);
  obj.__proto__ = Constructor.prototype;
  Constructor.apply(obj, arguments);
  return obj;

};

let test = objectFactory(Otaku,'test1',11)
let test2 = newTest(Otaku,'test1',11 )

console.log(test.__proto__ === Otaku.prototype,  test instanceof Otaku, test.__proto__.Constructor)
console.log(test2.__proto__ === Otaku.prototype, test2 instanceof Otaku, test2.__proto__.Constructor)

但是结果出乎意料

true false undefined
true true undefined

我认为应该返回TRUE的“ Otaku测试实例”,但结果为FALSE,所以我比较了“ test。 proto ”和“ Otaku.prototype”之间的区别,即结果为TRUE。我想原因是空的,但我不明白,我需要帮助

0 个答案:

没有答案