JavaScript class and inheritance: prototype attribute

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:

 
 
  1. // Constructor 
  2. Function Person (name ){
  3. This. Name = name;
  4. }
  5. // Define the prototype of Person. attributes in the prototype can be referenced by custom objects. 
  6. Person. prototype = {
  7. GetName: function (){
  8. Return This. Name;
  9. }
  10. }
  11. Var zhang =NewPerson ("ZhangSan");
  12. Console. log (zhang. getName ());// "ZhangSan" 

As an analogy, we consider the JavaScript data type-String), Number), Array), Object), Date) and so on. We have reason to believe that these types are implemented as constructors in JavaScript, such:

 
 
  1. // Define the constructor of the array as a predefined type of JavaScript 
  2. Function Array (){
  3. //... 
  4. }
  5.  
  6. // Initialize an array instance 
  7. Var arr1 =NewArray (1,56,34,12);
  8. // However, we prefer the following syntax definition: 
  9. 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, it is understandable that all the inherent Data Types in JavaScript have the read-only prototype attribute: Because if the prototype attribute of these types is modified, the predefined methods will disappear ), however, we can add our own extension methods to it.

 
 
  1. // Extend a method for obtaining the minimum value to the Array of the inherent JavaScript type. 
  2. Array. prototype. min = function (){
  3. Var min =This[0];
  4. For(Var I =1; I <This. Length; I ++ ){
  5. If(This[I] <min ){
  6. Min =This[I];
  7. }
  8. }
  9. ReturnMin;
  10. };
  11.  
  12. // Call the min Method on any Array instance 
  13. 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 assumption that the min method has been extended to the Array prototype ):

 
 
  1. var arr = [1, 56, 34, 12];  
  2. var total = 0;  
  3. for (var i in arr) {  
  4.     total += parseInt(arr[i], 10);  
  5. }  
  6. console.log(total);   // NaN 

The solution is also simple:

 
 
  1. var arr = [1, 56, 34, 12];  
  2. var total = 0;  
  3. for (var i in arr) {  
  4.     if (arr.hasOwnProperty(i)) {  
  5.         total += parseInt(arr[i], 10);  
  6.     }  
  7. }  
  8. console.log(total);   // 103 
  1. JavaScript class and inheritance: this property
  2. Summary of three + 1 implementation methods of ExtJS Grid Tooltip
  3. Implementation of JavaScript asynchronous call framework chain
  4. JQuery-style chained call of JavaScript asynchronous call framework
  5. Code Implementation of the JavaScript asynchronous call framework

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.