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