第一个代码:
function MyConstructor() {}
var myobject = new MyConstructor();
MyConstructor.prototype = {};
[ myobject instanceof MyConstructor, // false - why?
myobject.constructor == MyConstructor, // true
myobject instanceof Object ] // true
即使MyConstructor.prototype
被替换myobject
仍然从Myconstructor.prototype
. 那么为什么是myobject instanceOf Myconstuctor
假的呢?
function MyConstructor() {}
MyConstructor.prototype = {};
var myobject = new MyConstructor();
myobject instanceof MyConstructor // true (it is because myobject still inherits from
// Myconstructor.prototype although it has been replaced)
第二:
function MyConstructor() {}
MyConstructor.prototype = {};
var myobject = new MyConstructor();
myobject.constructor == MyConstructor; // false (accepted )
因此,如果 myobject.constructor 是 Object 为什么第一个:示例没有指向它,那么myobject.constructor
仍然指向它,MyConstructor
因为Myconstructor.prototype
在第一个示例中发生了变化。
你能澄清一下吗?