javascript - Is Object.prototype an object and is prototype somehow not? -
when type object.prototype
chrome console displays object{}
if assign variable var proto = object.prototype
displays object{}
but if check it's prototype
var proto = object.prototype proto.prototype;
if proto
object{}
, proto.prototype
undefined means thatobject()
is object()
doesn't inherit anything?
also, same object.prototype.prototype
?
if object has prototype, prototype object. these objects chain until reach null, end of prototype chain.
// object constructor object.prototype; // object {}, prototype of `new object`s // object.prototype object object.getprototypeof(object.prototype); // null, @ end of chain
you should note, however, can't keep accessing obj.prototype
property, applicable constructors, consider
function foo() { } foo.prototype; // foo {} // vs (new foo).prototype; // undefined
the correct way find prototype of object using object.getprototypeof(obj)
,
object.getprototypeof(new foo) === foo.prototype; // true
it may of note legacy browsers may not support object.getprototypeof
, in case many offer property obj.__proto__
. however, try avoid using __proto__
outside of shim such browsers if need access prototype chain.
finally, using new
constructor isn't way create chain, can set them using object.create
var = object.create(null), b = object.create(a), // b inherit c = object.create(b); // c inherit b, hence a.foo = 'foo'; b.bar = 'bar'; instanceof object; // false a.bar; // undefined c.foo + c.bar === 'foobar'; // true
also consider
c.prototype; // undefined // vs object.getprototypeof(c) === b; // true
Comments
Post a Comment