我正在处理 JavaScript,但遇到了一些验证问题:
我想检查作为参数给出的变量是否是对象实例的实例。为了更清楚,这里有一个例子:
var Example = function () {
console.log ('Meta constructor');
return function () {
console.log ('Instance of the instance !');
};
};
var inst = new Example();
assertTrue(inst instanceof Example.constructor); // ok
var subInst = new inst();
assertTrue(subInst instanceof Example.constructor); // FAIL
assertTrue(subinst instanceof inst.constructor); // FAIL
如何检查 subInst
是 Example.{new}
的一个实例?还是 inst.constructor
?
最佳答案
subInst.__proto__ == inst.prototype
关于javascript - 如何在 Javascript 中检查对象实例的 instanceof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13006690/