本文主要是介绍数组方法重构,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
数组方法重构
- 一、mypop
- 二、myPush
- 三、myShift
- 四、myUnshift
- 五、myForEach
- 六、myEvery
- 七、mySome
- 八、myMap
- 九、myFilter
一、mypop
// mypop
Array.prototype.myPop = function () {
var temp = this[this.length - 1];
this.length--
return temp
}
var arr = [1, 2, 3, 4, 5];
console.log(arr); //[ 1, 2, 3, 4, 5 ]
var result = arr.myPop();
console.log(result); //5
console.log(arr) //[ 1, 2, 3, 4 ]
二、myPush
// myPush
Array.prototype.myPush = function () {
for (var i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i]
}
return this.length
}
var arr = [1, 2, 3, 4, 5];
console.log(arr); //[ 1, 2, 3, 4, 5 ]
var result = arr.myPush('hello');
console.log(result); //6
console.log(arr); //[ 1, 2, 3, 4, 5, 'hello' ]
三、myShift
// myShift
Array.prototype.myShift = function () {
var result = this[0];
for (i = 0; i < this.length; i++) {
this[i] = this[i + 1]
}
this.length--
return result
}
var arr = [1, 2, 3, 4, 5];
console.log(arr); //[ 1, 2, 3, 4, 5 ]
var result = arr.myShift();
console.log(result); //1
console.log(arr); //[ 2, 3, 4, 5 ]
四、myUnshift
// myUnshift
Array.prototype.myUnshift = function () {
var sum = this.length + arguments.length;
for (var 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];
console.log(arr); //[ 1, 2, 3 ]
var result = arr.myUnshift('hello', 'a', 'b');
console.log(result); //6
console.log(arr); //[ 'hello', 'a', 'b', 1, 2, 3 ]
五、myForEach
// myForEach
Array.prototype.myForEach = function (fun) {
for (var i = 0; i < this.length; i++) {
fun(this[i], i, this)
}
}
var arr = [1, 2, 3];
console.log(arr); //[ 1, 2, 3 ]
var result = arr.myForEach(function (item, index, arr) {
console.log(item, index, arr) //1 0 [ 1, 2, 3 ]
//2 1 [ 1, 2, 3 ]
//3 2 [ 1, 2, 3 ]
});
六、myEvery
// myEvery
Array.prototype.myEvery = function (fun, obj) {
for (var i = 0; i < this.length; i++) {
if (!(obj ? fun.bind(obj)(this[i]) : fun(this[i]))) {
return false
} //如果有第二个参数,需要修改this指向
//if(!fun(this[i])){
// return false
//} ---没有第二个参数的逻辑操作
}
return true
};
var arr = [1, 2, 3, 4, 5];
var result = arr.myEvery(function (item, index, arr) {
console.log(this) //{ name: 'zhangsan' }
return item > 1 //一项条件不满足 直接返回false
}, { name: 'zhangsan' }) //第二个参数可以是任意数据类型)
console.log(result) //false
七、mySome
// mySome
Array.prototype.mySome = function (fun, obj) {
for (var i = 0; i < this.length; i++) {
if ((obj ? fun.bind(obj)(this[i]) : fun(this[i]))) {
return true
}
}
return false
};
var arr = [1, 2, 3, 4, 5];
var result = arr.mySome(function (item, index, arr) {
console.log(this) //{ name: 'zhangsan' }{ name: 'zhangsan' } 有第二个参数,this指向第二个参数;这里this打印两次,首先没有执行条件前,打印了一次,后面数组项1不满足条件再打印一次,数组项2满足条件直接跳出判断不打印
return item > 1 //一项满足 直接返回true
}, { name: 'zhangsan' }) //第二个参数可以是任意数据类型)
八、myMap
// myMap
Array.prototype.myMap = function (fun, obj) {
var result = [];
for (i = 0; i < this.length; i++) {
//obj?test():test2()
result.push(obj ? fun.bind(obj)(this[i]) : fun(this[i]))
}
return result
}
var arr = [1, 2, 3, 4, 5];
var result = arr.myMap(function (item, index, arr) {
console.log(this)//没有第二个参数this指向全局对象node 里面是global html文档中指向window //有第二个参数this指向第二个参数
return item + 1
}, { name: 'zhangsan' })
console.log(result)
九、myFilter
// myFilter
Array.prototype.myFilter = function (fun, obj) {
var result = [];
for (i = 0; i < this.length; i++) {
if (obj ? fun.bind(obj)(this[i]) : fun(this[i])) {
result.push(this[i])
}
}
return result
}
var arr = [1, 2, 3, 4, 5];
var result = arr.myFilter(function (item, index, arr) {
console.log(this)//global //obj //打印五次
return item > 2
}, { name: 'zhangsan' })
console.log(result) // [ 3, 4, 5 ]
这篇关于数组方法重构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!