Home >Web Front-end >JS Tutorial >Description of prototype js class_prototype
The following is the description of prototype:
=====
constructor attribute
is applied to: Array object | Boolean object | Date object | Function object | Number object | Object object | String object
requires
version 2
returns a reference to the prototype of the object type.
objectName.prototype
objectName parameter is the name of the object.
Description
Use the prototype attribute to provide a set of basic functions of the object's class. New instances of an object "inherit" the operations assigned to the object's prototype.
For example, add a method to the Array object that returns the value of the largest element in the array. To accomplish this, declare the function, add it to Array.prototype, and use it.
function array_max( ){
var i, max = this[0];
for (i = 1; i {
if ( max max = this[i];
}
return max;
}
Array.prototype.max = array_max;
var x = new Array (1, 2, 3, 4, 5, 6);
var y = x.max();
After this code is executed, y saves the maximum value in the array x, or 6.
All JScript internal objects have a read-only prototype attribute. You can add functionality to a prototype as in this example, but the object cannot be assigned a different prototype. However, user-defined objects can be assigned to new prototypes.
The list of methods and properties for each internal object in this language reference indicates which ones are part of the object's prototype and which ones are not.