Object.prototype.__proto__
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see thecompatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
Warning:Changing the[[Prototype]]
of an object is, by the nature of how modern JavaScript engines optimize property accesses, currently a very slow operation in every browser and JavaScript engine. In addition, the effects of altering inheritance are subtle and far-flung, and are not limited to the time spent in theobj.__proto__ = ...
statement, but may extend toany code that has access to any object whose[[Prototype]]
has been altered. You can read more inJavaScript engine fundamentals: optimizing prototypes.
Note:The use of__proto__
is controversial and discouraged. Its existence and exact behavior have only been standardized as a legacy feature to ensure web compatibility, while it presents several security issues and footguns. For better support, preferObject.getPrototypeOf()
/Reflect.getPrototypeOf()
andObject.setPrototypeOf()
/Reflect.setPrototypeOf()
instead.
The__proto__
accessor property ofObject
instances exposes the[[Prototype]]
(either an object ornull
) of this object.
The__proto__
property can also be used in an object literal definition to set the object[[Prototype]]
on creation, as an alternative toObject.create()
. See:object initializer / literal syntax. That syntax is standard and optimized for in implementations, and quite different fromObject.prototype.__proto__
.
Syntax
obj.__proto__
Return value
If used as a getter, returns the object's[[Prototype]]
.
Exceptions
TypeError
Thrown if attempting to set the prototype of anon-extensible object or animmutable prototype exotic object, such as
Object.prototype
orwindow
.
Description
The__proto__
getter function exposes the value of the internal[[Prototype]]
of an object. For objects created using an object literal (unless you use theprototype setter syntax), this value isObject.prototype
. For objects created using array literals, this value isArray.prototype
. For functions, this value isFunction.prototype
. You can read more about the prototype chain inInheritance and the prototype chain.
The__proto__
setter allows the[[Prototype]]
of an object to be mutated. The value provided must be an object ornull
. Providing any other value will do nothing.
UnlikeObject.getPrototypeOf()
andObject.setPrototypeOf()
, which are always available onObject
as static properties and always reflect the[[Prototype]]
internal property, the__proto__
property doesn't always exist as a property on all objects, and as a result doesn't reflect[[Prototype]]
reliably.
The__proto__
property is just an accessor property onObject.prototype
consisting of a getter and setter function. A property access for__proto__
that eventually consultsObject.prototype
will find this property, but an access that does not consultObject.prototype
will not. If some other__proto__
property is found beforeObject.prototype
is consulted, that property will hide the one found onObject.prototype
.
null
-prototype objects don't inherit any property fromObject.prototype
, including the__proto__
accessor property, so if you try to read__proto__
on such an object, the value is alwaysundefined
regardless of the object's actual[[Prototype]]
, and any assignment to__proto__
would create a new property called__proto__
instead of setting the object's prototype. Furthermore,__proto__
can be redefined as an own property on any object instance throughObject.defineProperty()
without triggering the setter. In this case,__proto__
will no longer be an accessor for[[Prototype]]
. Therefore, always preferObject.getPrototypeOf()
andObject.setPrototypeOf()
for setting and getting the[[Prototype]]
of an object.
Examples
Using __proto__
function Circle() {}const shape = {};const circle = new Circle();// Set the object prototype.// DEPRECATED. This is for example purposes only. DO NOT DO THIS in real code.shape.__proto__ = circle;// Get the object prototypeconsole.log(shape.__proto__ === Circle); // false
function ShapeA() {}const ShapeB = { a() { console.log("aaa"); },};ShapeA.prototype.__proto__ = ShapeB;console.log(ShapeA.prototype.__proto__); // { a: [Function: a] }const shapeA = new ShapeA();shapeA.a(); // aaaconsole.log(ShapeA.prototype === shapeA.__proto__); // true
function ShapeC() {}const ShapeD = { a() { console.log("a"); },};const shapeC = new ShapeC();shapeC.__proto__ = ShapeD;shapeC.a(); // aconsole.log(ShapeC.prototype === shapeC.__proto__); // false
function Test() {}Test.prototype.myName = function () { console.log("myName");};const test = new Test();console.log(test.__proto__ === Test.prototype); // truetest.myName(); // myNameconst obj = {};obj.__proto__ = Test.prototype;obj.myName(); // myName
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-object.prototype.__proto__ |