Udostępnij za pośrednictwem


prototype Property

Returns a reference to the prototype for a class of objects.

object.prototype

Arguments

  • object
    Required. The name of an object.

Remarks

Use the prototype property to provide a base set of functionality to a class of objects. New instances of an object "inherit" the behavior of the prototype assigned to that object.

All intrinsic JScript objects have a prototype property that is read-only. Functionality may be added to the prototype, as in the example, but the object may not be assigned a different prototype. However, user-defined objects may be assigned a new prototype.

The method and property lists for each intrinsic object in this language reference indicate which ones are parts of the object's prototype, and which are not.

Note

The prototype property of a built-in object cannot be modified when a program is running in fast mode, the default for JScript. To compile a program that uses the prototype property from a command prompt, you must turn off the fast option by using /fast-. It is not safe to turn off the fast option in ASP.NET because of threading issues.

Example

Say you want to add a method to the Array object that returns the value of the largest element of the array. To do this, declare the function, add it to Array.prototype, and then use it.

function array_max()
{
    var high = this[0];
    for (var i = 0; i < this.length; i++)
    {
       high = Math.max(high, this[i]);
    }
    return high
}

Array.prototype.max = array_max;
var arr = new Array(10, 14, 12, 6, 4);

var s = arr.max();
print (s);
// Output: 14

Requirements

Version 2

Applies To:

Array Object| Boolean Object| Date Object| Function Object| Number Object| Object Object| String Object

See Also

Reference

constructor Property