<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> prototype extension methods based on built-in classes </title>
<body>
<script type= "Text/javascript" >
var ary = [A], a, a, a, a, a, a,,,,,,,,,,,,, and 34];//->array->object
Built-in method chaining: Execution completes the previous method, the returned result is still an instance of the current class, so you can proceed to the next method
Ary.sort (function (A, b) {
return a-B;
}). reverse (). Pop (). push (100);
Console.log (ary);
--Give the method the name of the time add a own prefix myxxx, to prevent the built-in method to cover out
Array.prototype.myDistinct = function mydistinct () {
This->ary: We're going to go to the heavy of that array
var obj = {};
for (var i = 0; i < this.length; i++) {
var cur = this[i];
if (obj[cur] = = cur) {
This[i] = this[this.length-1];
this.length--;
i--;
Continue
}
Obj[cur] = cur;
}
obj = null;
Return this;//-> to implement chained notation
};
Ary.mydistinct (). push (100);
Console.log (ary);
</script>
</body>
A prototype extension method based on built-in classes