Export (0) Print
Expand All

__proto__ Property (Object) (JavaScript)

Contains a reference to the internal prototype of the specified object.

object.__proto__

object

Required. The object on which to set the prototype.

The __proto__ property can be used to set the prototype for an object.

The object or function inherits all methods and properties of the new prototype, along with all methods and properties in the new prototype's prototype chain. An object can have only a single prototype (not including inherited prototypes in the prototype chain), so when you call the __proto__ property, you replace the previous prototype.

You can set the prototype only on an extensible object. For more info, see Object.preventExtensions Function (JavaScript).

Note Note

The __proto__ property name begins and ends with two underscores.

The following code example shows how to set the prototype for an object.

function Rectangle() {
}

var rec = new Rectangle();

if (console && console.log) {
    console.log(rec.__proto__ === Rectangle.prototype);  // Returns true
    rec.__proto__ = Object.prototype;
    console.log(rec.__proto__ === Rectangle.prototype);  // Returns false
}

The following code example shows how to add properties to an object by adding them to the prototype.

var proto = { y: 2 };

var obj = { x: 10 };
obj.__proto__ = proto;

proto.y = 20;
proto.z = 40;

if (console && console.log) {
    console.log(obj.x === 10);  // Returns true
    console.log(obj.y === 20);  // Returns true
    console.log(obj.z === 40);  // Returns true
}

The following code example adds properties to the String object by setting a new prototype on it.

var stringProp = { desc: "description" };

String.__proto__ = stringProp;
var s1 = "333";
var s2 = new String("333");

if (console && console.log) {

    console.log(String.desc === "description"); // Returns true
    console.log(s1.desc === "description");     // Returns false
    console.log(s2.desc === "description");     // Returns false

    s1.__proto__ = String;  // Can't be set.
    s2.__proto__ = String;

    console.log(s1.desc === "description"); // Returns false
    console.log(s2.desc === "description"); // Returns true
}

Supported in the Internet Explorer 11 standards document mode. Also supported in Store apps (Windows 8.1 and Windows Phone 8.1). See Version Information. Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards. Not supported in Windows 8.

Show:
© 2014 Microsoft