超级实用手写 JavaScript 方法

1.数组去重方法
ES6 Set关键字
const _uniq = array => {
    return [...new Set(array)]
}
数组方法indexOf
const uniq = array => {
    let result = [];
    for(let i in array){
        if(result.indexOf(array[i]) === -1) result.push(array[i])
    }
    return result;
}
数组方法sort

纯数字,先排序再用相邻的对象进行比较

const uniq = array => {
    let result = [];
    array.sort()
    for(let i = 0;i<array.length;i++){
        if(array[i]!== array[i-1]) result.push(array[i])
    }
    return result;
}
双指针 方法

指针所指的值不相等的时候才加一,并改变原数组,最后用slice方法和慢指针取值

[-1,1,2,2]
const uniq = array => {
    let slow = 0,fast = 0;
    while(fast<array.length){
        if(array[slow] !== array[fast]){
            slow++;
            array[slow] = array[fast]
        }
        fast++
    }
    return array.slice(0,slow+1)
}
数组方法reduce
const uniq = array => {
    return array.reduce((a,b)=>a.includes(b)?a:[...a,b],[])
}
2.reduce方法
for循环 方法

上一次输出的值作为下一次输入的值

Array.prototype._reduce = function(fn){
    let num = 0;
    for(let i = 0;i<this[i];i++){
        num = fn(num,this[i],i,this)
    }
    return num;
}
3.map方法
for循环 方法
Array.prototype._map = function(fn){
    let result = [];
    for(let i in this){
        result[i] = fn(this[i])
    }
    return result;
}
数组方法reduce

上一次输出的值作为下一次输入的值,a初始值为[]

Array.prototype._map = function(fn){
    return this.reduce((a,b)=> [...a, fn(b)], [])
}
4.filter方法
for循环
Array.prototype._filter = function(fn){
    let result = [];
    for(let i in this){
        if(fn(this[i],i,this)) result.push(this[i])
    }
    return result;
}
数组方法reduce

上一次输出的值作为下一次输入的值,a初始值为[]

Array.prototype._filter = function(fn){
    return this.reduce((a, b) => fn(b) ? [...a, b] : a, []);
}
5.some方法
for循环 方法

有满足条件的就返回true

Array.prototype._some = function(fn){
    for(let i in this){
        if(fn(this[i])) return true;
    }
    return false;
}
数组方法reduce

上一次输出的值作为下一次输入的值,a初始值为[]

Array.prototype._filter = function(fn){
    return this.reduce((a, b) => fn(b) ? [...a, b] : a, []);
}
6.flat方法
while 循环+ some方法

判断数组元素中是否还有数组,使用concat拼接,使数组扁平化

function flat(arr) {
    while (arr.some(item=> Array.isArray(item))) {
        arr = [].concat(...arr)
    }
    return arr
}
reduce 方法

上一次输出的值作为下一次输入的值,a初始值为[]

function flat(arr) {
  return arr.reduce((a, b) => a.concat(Array.isArray(b) ? flat(b) : b), [])
};
使用堆栈避免递归 方法

使用数据结构栈的特性取代递归操作,减少时间复杂度

function flat(arr) {
    const result = []; 
    const stack = [].concat(arr);
    while (stack.length !== 0) {
        const val = stack.pop(); 
        if (Array.isArray(val)) stack.push(...val) 
        else result.unshift(val)
    }
    return result;
}

7.字符串全排列

递归 方法
const _permute = string => {
    if(string.length<2) return [string];
    let result = [];
    for(let i of string){
        let arr = string.split('').filter(e=>e!==i);
        _permute(arr.join('')).forEach(item=>{
            result.push(item+i)
        })
    }
    return result;
}
8.instanceof
const _instanceof = (target, Fn) => {
    while(true){
    if(target === null) return false;
    if(target.__proto__ === Fn.prototype) return true;
    target = target.__proto__
}
9.实现函数 call 方法
Function.prototype._call = function(context,...arg){
    if(!context)  context !== window;
    let fn = Symbol();
    context[fn] = this;
    return context[fn](...arg)
}
10.实现函数 bind 方法
Function.prototype._bind = function(context,...args){
    let _this = this;
    return function(...innerArg){
        return _this.call(context,args.concat(innerArg))
    }
}
11.防抖节流柯里化
防抖

search搜索联想,用户在不断输入值时,用防抖来节约请求资源(在输入完毕后执行)

const debounce = (fn,delay = 1000)=>{
    let timer;
    return function(){
        let args = argumens;
        clearTimeout(timer)
        timer = setTimeout(()=>{
            fn.apply(this,args)
        },delay)
    }
}
节流

——鼠标不断点击触发,单位时间内只触发一次

const throttle = (fn,delay = 1000)=>{
    let flag = true;
    return function(){
        if(!flag) return;
        flag = false;
        setTimeout(()=>{
            fn();
            flag = ture;
        },delay)
    }
}
柯里化
const curryIt = (fn)=>{
    let len = fn.length,args = [];
    let result = function(arg){
        args.push(arg);
        len--;
        if(len<=0) return fn.apply(this,args);
        else return result;
    }
    return result;
}
let fn = (a, b, c)=>  a + b + c; 
curryIt(fn)(1)(2)(3); // 6
12.new 关键字
const _new = function(fn,...args) {
    let obj = {}
    obj.__proto__ = fn.prototype
    fn.call(obj,...args)
    return obj
}
13.深浅拷贝
浅拷贝

只复制指向某个对象的指针,而不复制对象本身,新旧对象共享一块内存

利用 = 赋值操作符实现浅拷贝
深拷贝

复制并创建一个一摸一样的对象,不共享内存,修改新对象,旧对象保持不变

const _cloneDeep = (target, map = new Map()) => {
    if(target !== null && typeof target === 'object'){
        let result = Array.isArray(target)?[]:{};
        if(map.has(target)) return map.get(target);
        map.set(target,result);
        for(let i in target){
            result[i] = _cloneDeep(target[i],map);
        }
        return result;
    }
    return target;
}
14.发布订阅模式
class EventEmitter {
    constructor(){
        this.eventList = {}
    }
    on(event,fn){
        let fns = this.eventList[event] || [];
        fns.push(fn);
        this.eventList[event] = fns;
    }
    emit(event,...message){
        let fns = this.eventList[event] ||[];
        if(!fns) return;
        fns.forEach(item=>{
            item(...message)
        })
    }
}
15.斐波那契数列
function fibonacci(n) {
    if(n<3) return 1;
    return fibonacci(n-1) + fibonacci(n-2)
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容