JavaScript removes duplicate values from the array

Source: Internet
Author: User

Using the prototype function (prototype), you can define some very convenient custom functions, and implement various custom functions.

The prototype function (prototype) in JavaScript works by creating a prototype property for each time a new function is declared in JavaScript. All prototype properties automatically get the Constractor property without additional conditions, and constructor contains a pointer to the function that the prototype attribute belongs to (that is, the constructor callback constructor itself). Jingle County wei Bearing hardware

For instance, Fruit.prototype.constructor points to Fruit. And you can add more properties and methods to it through this constructor.

When the constructor is called to create a new instance, the instance contains a pointer to the constructor's prototype function. At this point we don't have to worry about what the inside pointer really is (the pointer does have a name: __proto__ is estimated to be the name of the prototype ~\ (≧▽≦)/~), just remember its point (the prototype function that points to the constructor). It is important to note that this __proto__ only exists between the function instance and the constructor's prototype function, not between the instance and the constructor.

The following is a custom 3 function using prototype, which is to remove duplicate values from the array, and to find the maximum and minimum values in the array.

The array is defined as: var arr = [2,1,3,2,1,4,3,4,2,1];

The program code is:

<script type= "text/javascript" >array.prototype.unique = function () {     var a = {};     var len = this.length;     for (var i=0; i < Len; i++)  {         if (typeof a[this[i] = = "undefined")         a[this[i]] = 1;       }       this.length = 0;       for (var i in a)       this[this.length] = i;       return this;     }  Array.prototype.max = function () {   //MAX value return Math.max.apply ({},this)}array.prototype.min = function () {   / /min return Math.min.apply ({},this)}var arr = [2,1,3,2,1,4,3,4,2,1];var btn1 = document.getElementById ("btn1"); Btn1.onclick = function () {arr.unique (); alert (arr.tostring ());} var btn2 = document.getElementById ("btn2"), Btn2.onclick = function () {alert (Arr.max ());} var btn3 = document.getElementById ("Btn3"), Btn3.onclick = function () {alert (arr.min ());} </script>

JavaScript removes duplicate values from the array

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.