JS Data Prototype Properties _ Basic knowledge

Source: Internet
Author: User

Material: Prototype Property
Returns a reference to the object type prototype.
Objectname.prototype
The objectname parameter is the name of the object.

Description
Use the prototype property to provide a set of basic functions for the object's class. The operation of the new instance of the object, "inherit", gives the object a prototype.

For example, to add a method that returns the maximum element value in an array for the array object. To do this, declare the function, add it to the Array.prototype, and use it.

Copy Code code as follows:

function Array_max () {
var i, max = this[0];
for (i = 1; i < this.length; i++)
{
if (Max < this[i])
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 the code executes, Y saves the maximum value in the array x, or says 6.

All JScript internal objects have read-only prototype properties. You can add functionality to the prototype as in this example, but the object cannot be given a different prototype. However, user-defined objects can be assigned to a new prototype.

The list of methods and attributes for each internal object in the language reference indicates which parts of the object's prototype and which are not.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.