导航
cb(value, index, array)
var arr = [
{
name: 'Lance',
age: 20,
},
{
name: 'GC',
age: 23
},
{
name: 'Shank',
age: 25
}
];
var obj = {
name: 'Lance',
age: 23
}
arr.forEach(function(value, index, arr) {
console.log(this); // 默认window
console.log(value, index, arr);
}, obj);
Array.prototype.myForEach = function(cb) {
var _arr = this,
_len = _arr.length,
_caller = arguments[1] || window;
for (var i = 0; i < _len; i++) {
cb.call(_caller, _arr[i], i, _arr);
}
}
var arr = [
{
name: 'Lance',
age: 20,
},
{
name: 'GC',
age: 23
},
{
name: 'Shank',
age: 25
}
];
var obj = {
name: 'Lance',
age: 23
}
var newArr = arr.map(function(item, index, arr) {
item.name = this.name;
return item; // 必须返回,否则数组里边成员都是 undefined
}, obj);
console.log(newArr, arr);
Array.prototype.myMap = function(cb) {
var _arr = this,
_len = _arr.length,
_caller = arguments[1] || window,
newArr = [];
for (var i = 0; i < _len; i++) {
// var item = deepClone(_arr[i]); // 如果要实现深拷贝,可以加此行代码,同时把下边 _arr[i] 替换为 item
newArr.push(cb.call(_caller, _arr[i], i, _arr));
}
return newArr;
}
var arr = [
{
name: 'Lance',
age: 20,
},
{
name: 'GC',
age: 23
},
{
name: 'Shank',
age: 25
}
];
var obj = {
name: 'Lance',
age: 23
}
var newArr = arr.filter(function(item, index, arr) {
console.log(item, index, arr, this);
return item.age > 20;
}, obj);
console.log(newArr, arr);
Array.prototype.myFilter = function(cb) {
var _arr = this,
_len = _arr.length,
_caller = arguments[1] || window,
newArr = [];
for (var i = 0; i < _len; i++) {
// var item = deepClone(_arr[i]); // 如果要实现深拷贝,可以加此行代码,同时把下边 _arr[i] 替换为 item
cb.call(_caller, _arr[i], i, _arr) && newArr.push(_arr[i]);
}
return newArr;
}
var arr = [
{
name: 'Lance',
age: 20,
},
{
name: 'GC',
age: 23
},
{
name: 'Shank',
age: 25
}
];
var obj = {
name: 'Lance',
age: 23
}
var newArr = arr.reduce(function(pre, item, index, arr) {
item.age >= 23 && pre.push(item);
return pre;
}, []);
console.log(newArr);
Array.prototype.myReduce = function(cb, initialValue) {
var _arr = this,
_len = _arr.length,
_caller = window;
for(var i = 0; i < _len; i++) {
// var item = deepClone(_arr[i]); // 如果要实现深拷贝,可以加此行代码,同时把下边 _arr[i] 替换为 item
initialValue = cb.call(_caller, initialValue, _arr[i], i, _arr);
}
return initialValue;
};
var arr = [
{
name: 'Lance',
age: 20,
},
{
name: 'GC',
age: 23
},
{
name: 'Shank',
age: 25
}
];
var obj = {
name: 'Lance',
age: 23
}
var newArr = arr.reduceRight(function(pre, item, index, arr) {
item.age >= 23 && pre.push(item);
return pre;
}, []);
console.log(newArr);
Array.prototype.myReduceRight = function(cb, initialValue) {
var _arr = this,
_len = _arr.length,
_caller = window;
for(var i = _len - 1; i >= 0; i--) {
// var item = deepClone(_arr[i]); // 如果要实现深拷贝,可以加此行代码,同时把下边 _arr[i] 替换为 item
initialValue = cb.call(_caller, initialValue, _arr[i], i, _arr);
}
return initialValue;
};
var arr = [
{
name: 'Lance',
age: 20,
},
{
name: 'GC',
age: 23
},
{
name: 'Shank',
age: 25
}
];
var obj = {
name: 'Lance',
age: 23
}
var res = arr.every(function(item, index, arr) {
return item.age < 23;
}, obj);
console.log(res);
Array.prototype.myEvery = function(cb) {
var _arr = this,
_len = _arr.length,
_caller = arguments[1] || window;
for (var i = 0; i < _len; i++) {
// var item = deepClone(_arr[i]); // 如果要实现深拷贝,可以加此行代码,同时把下边 _arr[i] 替换为 item
if (!cb.call(_caller, _arr[i], i, _arr)) {
return false;
}
}
return true;
};
var arr = [
{
name: 'Lance',
age: 20,
},
{
name: 'GC',
age: 23
},
{
name: 'Shank',
age: 25
}
];
var obj = {
name: 'Lance',
age: 23
}
var res = arr.some(function(item, index, arr) {
return item.age <= 20;
}, obj);
console.log(res);
Array.prototype.mySome = function(cb) {
var _arr = this,
_len = _arr.length,
_caller = arguments[1] || window;
for (var i = 0; i < _len; i++) {
// var item = deepClone(_arr[i]); // 如果要实现深拷贝,可以加此行代码,同时把下边 _arr[i] 替换为 item
if (cb.call(_caller, _arr[i], i, _arr)) {
return true;
}
}
return false;
}