Javascript class and inherit prototype attributes

Source: Internet
Author: User

We have used prototype property simulation class and inheritance implementation in Chapter 1. The prototype attribute is essentially a JavaScript Object. Each function has a default prototype attribute.
If this function is used to create a custom object, we call it a constructor. For example, the following simple scenario: CopyCode The Code is as follows: // Constructor
Function person (name ){
This. Name = Name;
}
// Define the prototype of person. attributes in the prototype can be referenced by custom objects.
Person. Prototype = {
Getname: function (){
Return this. Name;
}
}
VaR Zhang = new person ("zhangsan ");
Console. Log (Zhang. getname (); // "zhangsan"

As an analogy, we consider the data types in Javascript, such as string, number, array, object, and date. We have reason to believe that these types are implemented as constructors in Javascript, such:Copy codeThe Code is as follows: // defines the array constructor, which is a predefined type of JavaScript.
Function Array (){
//...
}
// Initialize an array instance
VaR arr1 = new array (1, 56, 34, 12 );
// However, we prefer the following syntax definition:
VaR arr2 = [1, 56, 34, 12];

At the same time, many methods for Array Operations (such as Concat, join, and push) should also be defined in the prototype attribute.
In fact, all the inherent Data Types in Javascript have the read-only prototype attribute (this is understandable: If you modify the prototype attribute of these types, but we can add our own extension method to it.Copy codeThe Code is as follows: // extend a method to obtain the minimum value of an array of the inherent JavaScript type.
Array. Prototype. min = function (){
VaR min = This [0];
For (VAR I = 1; I <this. length; I ++ ){
If (this [I] <min ){
Min = This [I];
}
}
Return min;
};
// Call the min Method on any array instance
Console. Log ([1, 56, 34, 12]. Min (); // 1

Note: There is a trap. After adding an extension method to the array prototype, this extension method will also be recycled when the for-in loop array is used.
The following code illustrates this (assuming that the min method has been extended to the array prototype ):Copy codeThe Code is as follows: var arr = [1, 56, 34, 12];
VaR Total = 0;
For (var I in ARR ){
Total + = parseint (ARR [I], 10 );
}
Console. Log (total); // Nan

The solution is also simple:Copy codeThe Code is as follows: var arr = [1, 56, 34, 12];
VaR Total = 0;
For (var I in ARR ){
If (ARR. hasownproperty (I )){
Total + = parseint (ARR [I], 10 );
}
}
Console. Log (total); // 103

Related Article

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.