目录
pop
Array.prototype.Pop=function(thisArg){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
if (thisArg.length === 0) return;
var item = thisArg[thisArg.length - 1];
thisArg.length--;
return item;
}
var arr=[1,2,3,4]
var fn = arr.Pop(arr);
console.log(fn);
push
Array.prototype.Push=function(thisArg,arguments){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
for (var i = 1; i < arguments.length; i++) {
thisArg[thisArg.length] = arguments[i];
}
return thisArg.length;
}
var arr=[1,2,3,4]
var arr1=[5,6,7,8]
var rel = arr.Push(arr,arr1);
console.log(rel);
shift
Array.prototype.Shift=function(thisArg){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
if (thisArg.length === 0) return;
var item = thisArg[0];
for (var i = 0; i < thisArg.length - 1; i++) {
thisArg[i] = thisArg[i + 1];
}
thisArg.length--;
return item;
}
var arr=[1,2,3,4]
var rel = arr.Shift(arr);
console.log(rel);
unshift
Array.prototype.Unshift=function(thisArg,arguments){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("请给入正确的数组");
for (var i = arguments.length + thisArg.length - 1; i >= 0; i--) {
if (i - (arguments.length) >= 0) thisArg[i] = thisArg[i - (arguments.length)];
else thisArg[i] = arguments[i];
}
return thisArg.length;
}
var arr=[1,2,3,4]
var arr1=[5,6,7,8,9]
var rel = arr.Unshift(arr,arr1);
console.log(arr,rel);
concat
Array.prototype.Concat=function(thisArg){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
var arr = [];
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] && arguments[i].constructor === Array) {
for (var j = 0; j < arguments[i].length; j++) {
if (j in arguments[i]) arr[arr.length] = arguments[i][j];
else arr.length++;
}
} else arr[arr.length] = arguments[i]
}
return arr;
}
var arr=[1,2,3,4]
var arr1=[5,6,7,8,9]
var rel = arr.Concat(arr,arr1);
console.log(rel);
join
Array.prototype.Join=function(thisArg,separator){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
if (thisArg.length == 0) return "";
if (separator == undefined) separator = ",";
var str = "";
for (var i = 0; i < thisArg.length - 1; i++) {
str += thisArg[i] + separator;
}
return str + thisArg[thisArg.length - 1];
}
var arr=[1,2,3,4]
var rel = arr.Join(arr,'.');
console.log(rel);
slice
Array.prototype.Slice=function(thisArg, start, end){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
if (thisArg.length === 0) return [];
if (start === undefined) start = 0;
if (end === undefined) end = thisArg.length;
start = ~~start;
end = ~~end;
if (start < 0) start = (start + thisArg.length < 0 ? 0 : start + thisArg.length);
if (end < 0) end = (end + thisArg.length < 0 ? 0 : end + thisArg.length)
if (end > thisArg.length) end = thisArg.length;
var arr = [];
for (var i = start; i < end; i++) {
arr[arr.length] = thisArg[i];
}
return arr;
}
var arr=[1,2,3,4]
var rel = arr.Slice(arr,2);
console.log(rel);
every
Array.prototype.Every=function(thisArg){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
var bool;
for (var i = 0; i < thisArg.length; i++) {
bool = thisArg[i]>2 ? true : false
if(!bool) return bool;
}
return bool;
}
var arr=[1,2,3,4]
var rel = arr.Every(arr);
console.log(rel);
some
Array.prototype.Some=function(thisArg){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
var bool;
for (var i = 0; i < thisArg.length; i++) {
bool = thisArg[i]>2 ? true : false
if(bool) return bool;
}
return bool;
}
var arr=[1,2,3,4]
var rel = arr.Some(arr);
console.log(rel);
map
Array.prototype.Map=function(thisArg){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
var arr = [];
for (var i = 0; i < thisArg.length; i++) {
arr[arr.length] = thisArg[i]+20;
}
return arr;
}
forEach
Array.prototype.ForEach=function(thisArg){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
for (var i = 0; i < thisArg.length; i++) {
console.log(thisArg[i]);
}
}
var arr=[1,2,3,4]
var rel = arr.ForEach(arr);
filter
Array.prototype.Fillter=function(thisArg){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数 组!");
var arr = [];
for (var i = 0; i < thisArg.length; i++) {
if(thisArg[i]>2){
arr[arr.length] = thisArg[i]
}
}
return arr;
}
var arr=[1,2,3,4]
var rel = arr.Fillter(arr);
console.log(rel);
find
Array.prototype.Find=function(thisArg){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
for (var i = 0; i < thisArg.length; i++) {
if (thisArg[i]>1) return thisArg[i];
}
}
var arr=[1,2,3,4]
var rel = arr.Find(arr);
console.log(rel);
findIndex
Array.prototype.FindIndex=function(thisArg,num){
if (!thisArg || thisArg.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
for (var i = 0; i < thisArg.length; i++) {
if(thisArg[i]==num) return i;
}
}
var arr=[1,2,3,4]
var rel = arr.FindIndex(arr,2);
console.log(rel);
indexOf
Array.prototype.IndexOf=function(array, searchElement, start){
if (!array || array.constructor !== Array) throw new TypeError("这不是一个正确的数组");
if (start === undefined) start = 0;
start = ~~start;
for (var i = start; i < array.length; i++) {
if (array[i] === searchElement) return i;
}
return -1;
}
var arr=[1,2,3,4]
var rel = arr.IndexOf(arr,6);
console.log(rel);
lastindexOf
Array.prototype.LastIndexOf=function(array, searchElement, start){
if (!array || array.constructor !== Array) throw new TypeError("这不是一个正确的数组");
if (start === undefined) start = array.length - 1;
start = ~~start;
for (var i = start; i >= 0; i--) {
if (array[i] === searchElement) return i;
}
return -1;
}
var arr=[1,2,3,4]
var rel = arr.LastIndexOf(arr,3);
console.log(rel);
copyWithin
Array.prototype.CopyWithin=function(array, target, start, end){
if (!array || array.constructor !== Array) throw new TypeError("这不是一个正确的数 组!");
if (start === undefined) start = 0;
if (end === undefined) end = array.length;
start = ~~start;
end = ~~end;
if (target < 0) target = (target + array.length < 0 ? 0 : target + array.length);
if (start < 0) start = (start + array.length < 0 ? 0 : start + array.length);
if (end < 0) end = (end + array.length < 0 ? 0 : end + array.length);
var arr = [];
for (var i = start; i < end; i++) {
arr[i] = array[i];
}
for (var j = target; j < array.length; j++, start++) {
if (start < end) array[j] = arr[start];
else return
}
return array;
}
var arr=[1,2,3,4]
var rel = arr.CopyWithin(arr,3);
console.log(rel);
includes
Array.prototype.Includes=function(array, seachElement){
if (!array || array.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
for (var i = 0; i < array.length; i++) {
if (array[i] === seachElement) return true;
}
return false;
}
var arr=[1,2,3,4]
var rel = arr.Includes(arr,3);
console.log(rel);
revers
Array.prototype.Revers=function(array){
if (!array || array.constructor !== Array) throw new TypeError("这不是一个正确的数组!");
if (array.length < 2) return array;
for (var i = 0; i < ~~(array.length / 2); i++) {
var temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
return array;
}
var arr=[1,2,3,4]
var rel = arr.Revers(arr);
console.log(rel);
sort
Array.prototype.Sort=function(arr){
for(var i=0; i < arr.length; i++){
var minIndex = 0;
for (var j = i + 1; j < arr.length; j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j;
}
}
if(minIndex!==i){
var temp=arr[i];
arr[i]=arr[minIndex];
arr[minIndex]=temp;
}
}
return arr;
}
var arr=[4,3,2,1]
var rel = arr.Sort(arr);
console.log(rel);
currying
Function.prototype.cuttying = function(){
var arr = [];
var fn = this;
return function(){
if(arguments.length>0){
[].push.apply(arr,arguments);
retrun arguments.callee;
}else{
return fn.apply(null,arr);
}
}
}
function getSum(){
return Array.from(arguments).reduce((v,t)=>v+t);
}
var fn = getSum.currying();
fn(1,2,3);
fn(4,5,6);
var s=fn();
console.log(s)
bind
Function.prototype.bind_1 = function(thisArg){
var fn = this;
return function(){
return fn.apply(thisArg,arguments);
}
}
var obj = {
a:1,
b:function(){
document.addEventListener("click",this.clickHandler.bind_1(this));
},
clickhandler:function(e){
console.log(this);
}
}
obj.b()