我有这样的代码
'use strict';
let log = console.log;
let cParent = function () {
this.property1 = arguments[0] || null;
this.property2 = arguments[1] || null;
this.log = function () {
log('log: ', this.property1);
}
};
let obj1 = new cParent(4, 5);
log(obj1.prototype);
log(Object.getPrototypeOf(obj1));
log(cParent.prototype);
log(Object.getPrototypeOf(cParent));
得到的结果是
undefined
{}
{}
[Function]
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
obj.__prototype__和Object.getPrototypeOf(obj)的结果才会相同。而他们通常又与obj.constructor.prototype相同。在你的例子中,也就是
cParent.prototype === obj1.__proto__;cParent.prototype === Object.getPrototypeOf(obj1)。一个是找爸爸一个是找老公 当然不同了