第一个代码:
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
在第一个示例中已更改。
你能澄清一下吗?