javascript - why isn't Object.prototype === to myNewObj.prototype? - Stack Overflow

admin2025-04-19  0

So if you look at this fiddle / you can see that

var me = { fName: "ken", lName: "n" };

console.log(Object.prototype === Object.getPrototypeOf(me));

returns true. Why doesn't

console.log(Object.prototype === me.prototype);

Given that I created the "me" object as an object literal sure enough it's prototype should be Object.prototype and the first line would seem to confirm that.

So if you look at this fiddle http://jsfiddle/r0k3t/z8f2N/1/ you can see that

var me = { fName: "ken", lName: "n" };

console.log(Object.prototype === Object.getPrototypeOf(me));

returns true. Why doesn't

console.log(Object.prototype === me.prototype);

Given that I created the "me" object as an object literal sure enough it's prototype should be Object.prototype and the first line would seem to confirm that.

Share Improve this question edited Dec 19, 2012 at 15:11 Drew Noakes 312k172 gold badges698 silver badges765 bronze badges asked Dec 19, 2012 at 15:06 KennKenn 2,7693 gold badges30 silver badges61 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 18
Object.prototype === me.constructor.prototype; // true

I let you guess now how getPrototypeOf works :-)

Also, the non-standard-yet-but-works-almost-everywhere solution (thanks jAndy):

Object.prototype === me.__proto__; // true
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745001218a279237.html

最新回复(0)