Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Object.prototype.__proto__

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

js
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 asObject.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__

js
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
js
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
js
function ShapeC() {}const ShapeD = {  a() {    console.log("a");  },};const shapeC = new ShapeC();shapeC.__proto__ = ShapeD;shapeC.a(); // aconsole.log(ShapeC.prototype === shapeC.__proto__); // false
js
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__

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp