Array.prototype.myShift = function(){
var first = this[0];
for(var i=0;i<this.length;i++){
this[i] = this[i+1];
}
this.length--;
return first
}
var arr = [1,2,3,4,5];
var res = arr.myShift();
console.log(res,arr);
Array.prototype.myUnshift=function(){
var sum = this.length + arguments.length;
for(i=sum;i>0;i--){
if(i>arguments.length){
this[i - 1] = this[i - 1 - arguments.length]
}else{
this[i - 1] = arguments[i-1]
}
}
return sum
}
var arr = [1,2,3,4,5];
var res = arr.myUnshift('tom','larry');
console.log(res,arr);
Array.prototype.myForEach = function(callback){
for(var i=0;i<this.length;i++){
callback(this[i],i,this)
}
}
var arr = [1,2,3,4,5];
arr.myForEach(function(item,index,arr){
console.log(item,index,arr)
});
Array.prototype.myevery = function (callback) {
for (var i = 0; i < this.length; i++) {
if (!callback(this[i])) {
return false
}
}
return true
};
var arr = [1, 2, 3, 4, 5];
var res = arr.myevery(function (item) {
console.log('every');
return item > 2
})
console.log(res, arr);
Array.prototype.mysome = function (callback) {
for (var i = 0; i < this.length; i++) {
if (callback(this[i])) {
return true
}
}
return false
};
var arr = [1, 2, 3, 4, 5];
var res = arr.mysome(function (item) {
console.log('some');
return item > 2
})
console.log(res, arr);
Array.prototype.mymap = function (callback) {
var newArr=[]
for (var i = 0; i < this.length; i++) {
newArr.push(callback(this[i]))
}
return newArr
}
var arr = [1, 2, 3, 4, 5];
var res = arr.mymap(function (item) {
return item * 10
})
console.log(res, arr);
Array.prototype.myfilter = function (callback) {
var newArr = []
for (var i = 0; i < this.length; i++) {
if (callback(this[i])) {
newArr.push(this[i])
}
}
return newArr
}
var arr = [11, 22, 5, 36, 12];
var res = arr.myfilter(function (item) {
return item > 15
})
console.log(res, arr);