Export (0) Print
Expand All

Object.getPrototypeOf Function (JavaScript)

Returns the prototype of an object.

Object.getPrototypeOf(object)

object

Required. The object that references the prototype. This can be a native JavaScript object or a DOM object.

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.

function getPrototypeFromObject() {

    // 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 pr = Object.getPrototypeOf(spaghetti);

    // Add a property to the prototype and validate that
    // the original object has the property.
    pr.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 = (pr === Pasta.prototype);
    document.write(result + " ");

    // Verify that prototype obtained from the object
    // is a prototype of the original object.
    // The commented statement below is equivalent
    // to the uncommented statement below.
    var result = pr.isPrototypeOf(spaghetti);
    // var result = Pasta.prototype.isPrototypeOf(spaghetti);
    document.write(result);

    // Output: carbohydrates true true
}

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

function validateTypes() {

    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
}

The following example creates an object that is a copy of another object. In the clone function, the Object.getPrototypeOf function obtains the prototype of the object for use by the Object.create function.

function processClone() {

    // Create a constructor function and add a property
    // to its prototype.
    function Pasta(grain, width) {
        this.grain = grain;
        this.width = width;
    }
    Pasta.prototype.foodgroup = "carbohydrates";

    // Create an object from the Pasta constructor and
    // make the object non-extensible.
    var spaghetti = new Pasta("wheat", 0.2);
    Object.preventExtensions(spaghetti);

    // Copy the object and its properties.
    var newObj = clone(spaghetti);

    // Show the properties of the new object and those
    // of its prototype.
    showProperties(newObj);
    var pr = Object.getPrototypeOf(newObj);
    showProperties(pr);

    // Verify that the new object has Pasta as its prototype.
    var result = Pasta.prototype.isPrototypeOf(spaghetti);
    document.write(result + " ");

    // Verify that the new object is non-extensible.
    var result = (Object.isExtensible(newObj) === false);
    document.write(result);

    // Output:
    //  grain: wheat
    //  width: 0.2

    //  constructor: function Pasta(grain, width) { this.grain = grain; this.width = width; }
    //  foodgroup: carbohydrates

    //  true true
}


// Create an object that is a copy of another object.
function clone(oldObj) {

    // Obtain the prototype of the original object and create
    // a new object by using that prototype.
    var pr = Object.getPrototypeOf(oldObj);
    var newObj = Object.create(pr);

    // Copy the properties of the original object to the new object.
    var names = Object.getOwnPropertyNames(oldObj);
    names.forEach(
        function (prop) {
            var desc = Object.getOwnPropertyDescriptor(oldObj, prop);
            Object.defineProperty(newObj, prop, desc);
        }
    );

    // If the original object is non-extensible, make the new object 
    // non-extensible.
    if (!Object.isExtensible(oldObj))
    {
        Object.preventExtensions(newObj);
    }

    // Return the new object.
    return newObj;
}

// Show the properties of an object.
function showProperties(obj) {
    var newLine = "<br />";

    var names = Object.getOwnPropertyNames(obj);
    names.forEach(
        function (prop) {
            document.write(prop + ": " + obj[prop]);
            document.write(newLine);
        }
    )
    document.write(newLine);
}

Supported in the Internet Explorer 9 standards document mode. See Version Information.

Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards.

Community Additions

Show:
© 2014 Microsoft