Windows Dev Center

Object.getPrototypeOf Function (JavaScript)

Returns the prototype of an object.

Object.getPrototypeOf(object)

object

Required. The object that references the prototype.

The prototype of the object argument. The prototype is also an object.

If the object argument is not an object, a TypeError exception is thrown.

The following example illustrates the use of the Object.getPrototypeOf function.

// Create a constructor function.
function Pasta(grain, width) {
    this.grain = grain;
    this.width = width;
}
// Create an object from the pasta constructor.
var spaghetti = new Pasta("wheat", 0.2);

// Obtain the prototype from the object.
var proto = Object.getPrototypeOf(spaghetti);

// Add a property to the prototype and validate that
// the original object has the property.
proto.foodgroup = "carbohydrates";
document.write(spaghetti.foodgroup + " ");

// Verify that the prototype obtained from the object
// is the same as the prototype of the constructor.
var result = (proto === Pasta.prototype);
document.write(result + " ");

// Verify that prototype obtained from the object
// is a prototype of the original object.
var result = proto.isPrototypeOf(spaghetti);
document.write(result);

// Output: carbohydrates true true

The following example uses the Object.getPrototypeOf function to validate data types.

var reg = /a/;
var result = (Object.getPrototypeOf(reg) === RegExp.prototype);
document.write(result + " ");

var err = new Error("an error");
var result = (Object.getPrototypeOf(err) === Error.prototype);
document.write(result);

// Output: true true

Supported in the following document modes: Internet Explorer 9 standards, Internet Explorer 10 standards, and Internet Explorer 11 standards. Also supported in Store apps (Windows 8 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.

Show:
© 2015 Microsoft