15个web前端自定义函数工具库
web前端开发
共 6218字,需浏览 13分钟
·
2021-02-19 13:29
来源 | http://www.fly63.com/article/detial/10164
1、call函数封装实现
// 手写call函数
function call(Fn,obj,...arg){
// 如果obj为null或者undefined,则指向window
if(obj === undefined || obj === null){
// globalThis是ES11的新特性,指向全局
obj = globalThis
}
//为obj添加临时方法
obj.temp = Fn
// 调用 temp 方法
let result = obj.temp(...arg)
// 删除obj 的 temp
delete obj.temp
return result
}
function add(a,b){
console.log(this);
return a + b + this.c
}
let obj = {
c:521
}
globalThis.c = 1314
console.log(call(add,obj,10,20)); //551
console.log(call(add,null,10,20)); //1344
// 手写call函数
Function.prototype.call = function(obj,...arg){
// 如果obj为null或者undefined,则指向window
if(obj === undefined || obj === null){
// globalThis是ES11的新特性,指向全局
obj = globalThis
}
//为obj添加临时方法
obj.temp = this
// 调用 temp 方法
let result = obj.temp(...arg)
// 删除obj 的 temp
delete obj.temp
return result
}
function add(a,b){
console.log(this);
return a + b + this.c
}
let obj = {
c:521
}
globalThis.c = 1314
console.log(add.call(obj,10,20)); //551
console.log(add.call(null,10,20)); //1344
2、apply函数封装实现
// 手写apply函数
Function.prototype.apply = function(obj,arg){
if(obj === null || obj === undefined){
obj = globalThis
}
obj.temp = this
let result = obj.temp(...arg)
delete obj.temp
return result
}
function add(a,b){
console.log(a+b+this.c);
}
let obj = {
c:1314
}
globalThis.c = 520
add.apply(obj,[10,20]) //1344
add.apply(null,[10,20]) //550
3、bind函数封装实现(bind不会立刻执行)
function bind(Fn,obj,...args){
if(obj === null || obj === undefined){
obj = globalThis
}
//bind返回一个函数,调用的时候执行方法
return function(...args2){
// 调用call方法
obj.temp = Fn
let result = obj.temp(...args,...args2)
delete obj.temp
return result
}
}
function add(a,b){
console.log(arguments);
console.log(a+b+this.c);
}
let obj = {
c:1314
}
globalThis.c = 520
bind(add,obj,10,20)() //1344
bind(add,null,10,20)(30,40) //550
4、函数节流
5、函数防抖
6、数组函数map封装实现
const arr = [1,2,3,4,5]
Array.prototype.map = function (callback) {
let result = []
for(let i = 0;i
result.push(callback(this[i],i))
}
return result
}
let arr2 = arr.map((item,index) => {
return item *10
})
console.log(arr2);
7、数组函数reduce封装实现
const arr = [1,2,3,4,5]
// 示例
let result = arr.reduce((res,value)=>{
return res + value
},0) //0为res初始值,value为arr的值
console.log(result); //15
Array.prototype.reduce = function(callback,value){
let result = value
for(let i = 0;i
result = callback(result,this[i])
}
return result
}
// 演示
let arr2 = arr.reduce((res,value)=>{
return res + value
},5)
console.log(arr2);
const arr = [1,2,3,4,5]
Array.prototype.filter2 = function(callback){
let arr = []
for(let i = 0;i
if(callback(this[i],i)){
arr.push(this[i])
}
}
return arr
}
let res = arr.filter2((item=>{
return item > 2
}))
console.log(res);
8、数组函数find封装实现
find():找到第一个满足测试函数的元素并返回那个元素的值,如果找不到,则返回undefined
const arr = [1,2,3,2005,4,1001]
// find()
Array.prototype.find = function(callback){
for(let i = 0;i
if(callback(this[i],i)){
return this[i]
}
}
return undefined
}
let res = arr.find((item=>{
return item > 3000
}))
console.log(res);
9、数组函数findIndex封装实现
// findIndex()
Array.prototype.findIndex2 = function(callback){
for(let i = 0;i
if(callback(this[i],i)){
return i
}
}
return -1
}
let res = arr.findIndex2((item=>{
return item > 1000
}))
console.log(res);
10、数组函数every封装实现
const arr = [1,2,3,4,5]
Array.prototype.every2 = function(callback){
for(let i = 0;i
let result = callback(this[i],i)
if(!result){
return false;
}
}
return true
}
const result = arr.every2(item=>{
return item > 0
})
console.log(result);
11、数组函数some封装实现
Array.prototype.some2 = function(callback){
for(let i = 0;i
let result = callback(this[i],i)
if(result){
return true
}
}
return false;
}
const result = arr.some2(item=>{
return item > 6
})
console.log(result);
12、数组去重
const arr = [1,2,3,4,5,2,4]
// 方法1 :forEach + indexOf
function unique(arr){
if(!Array.isArray(arr)){
return
}
let result = []
arr.forEach(item=>{
if(result.indexOf(item) === -1){
result.push(item)
}
})
return result
}
let result = unique(arr)
console.log(result);
// 方法2 forEach() + 对象容器
function unique2(arr){
let result = []
//声明空对象
const obj = {}
arr.forEach(item => {
if(obj[item] === undefined){
obj[item] = true
result.push(item)
}
})
console.log(obj);
return result
}
let result2 = unique2(arr)
console.log(result2);
//方法3:利用ES6语法:from + Set 或者 ... + Set
function unique3(arr){
return [...new Set(arr)]
// let result = Array.from(new Set(arr))
// return result
}
let result3 = unique3(arr)
console.log(result2);
13、数组合并和切片
let arr = [1,2,3]
Array.prototype.concat2 = function(...args){
let result = [...this,...args]
return result
}
const result = arr.concat2([4,5,6],7,8)
console.log(result);
数组切片slice()
Array.prototype.slice2 = function(begin,end){
if(this.length === 0){
return [];
}
//判断begin
begin = begin || 0
if(begin >= this.length){
return [];
}
//判断end
end = end || this.length
if(end
end = this.length
}
let result = []
this.forEach((item,index)=>{
if(index >= begin && index < end){
result.push(item)
}
})
return result
}
let sliceResult = arr.slice2(1,6)
console.log(sliceResult);
14、数组扁平化
let arr = [1,2,[3,4,[5,6]],7]
// 方法1
function falttenl(arr){
let result = []
arr.forEach(item => {
if(Array.isArray(item)){
result = result.concat(falttenl(item))
}else{
result = result.concat(item)
}
});
return result
}
console.log(falttenl(arr));
// 方法2
function flatten2(arr){
let result = [...arr]
while(result.some(item => Array.isArray(item))){
result = [].concat(...result)
}
return result
}
console.log(flatten2(arr));
15、数组分块
评论