自用前端笔试题

163 阅读1分钟

1、手写instanceof

instanceof作用: 判断一个实例是否是其父类或者祖先类型的实例。 instanceof 在查找的过程中会遍历左边变量的原型链,直到找到右边变量的 prototype**查找失败,返回 false

let myInstanceof = (target,origin) => {
     while(target) {
         if(target.__proto__===origin.prototype) {
            return true
         }
         target = target.__proto__
     }
     return false
 }
 let a = [1,2,3]
 console.log(myInstanceof(a,Array));  // true
 console.log(myInstanceof(a,Object));  // true

2. 实现数组的map方法

Array.prototype.myMap = function(fn, thisValue) {
     let res = []
     thisValue = thisValue||[]
     let arr = this
     for(let i in arr) {
        res.push(fn(arr[i]))
     }
     return res
 }

3. reduce实现数组的map方法

Array.prototype.myMap = function(fn,thisValue){
     const res = [];
     thisValue = thisValue||[];
     this.reduce(function(pre,cur,index,arr){
         return res.push(fn.call(thisValue,cur,index,arr));
     },[]);
     return res;
}

const arr = [2,3,1,5];
arr.myMap(function(item,index,arr){
 console.log(item,index,arr);
})

4. 手写数组的reduce方法

function reduce(arr, cb, initialValue){
     let num = initValue == undefined? num = arr[0]: initValue;
     let i = initValue == undefined10
     for (i; i< arr.length; i++){
        num = cb(num,arr[i],i)
     }
     return num
 }
 
 function fn(result, currentValue, index){
     return result + currentValue
 }
 
 const arr = [2,3,4,5]
 let b = reduce(arr, fn,10) 
 let c = reduce(arr, fn)
 console.log(b)   // 24

5. 数组扁平化

  1. es6提供的新方法 flat(depth)
let a = [1,[2,3,[4,[5]]]]; 
a.flat(Infinity);//flat可设置维度,也可无需知道数组的维度,直接将目标数组变成1维数组。depth的值设置为Infinity。
  1. 利用cancat

var arr1 = [123, [1234, [234]]];
 function flatten(arr) {
     const res = [];
     for (let i = 0, length = arr.length; i < length; i++) {
     if (Array.isArray(arr[i])) {
     res = res.concat(flatten(arr[i])); //concat 并不会改变原数组
     //res.push(...flatten(arr[i])); //扩展运算符 
     } else {
         res.push(arr[i]);
       }
     }
     return res;
 }
 flatten(arr1); //[1, 2, 3, 1, 2, 3, 4, 2, 3, 4]

6. 函数柯里化

柯里化的定义:接收一部分参数,返回一个函数接收剩余参数,接收足够参数后,执行原函数。 当柯里化函数接收到足够参数后,就会执行原函数,如何去确定何时达到足够的参数呢? 有两种思路:

  1. 通过函数的 length 属性,获取函数的形参个数,形参的个数就是所需的参数个数
  2. 在调用柯里化工具函数时,手动指定所需的参数个数

将这两点结合一下,实现一个简单 curry 函数:

/**
 * 将函数柯里化
 * @param fn    待柯里化的原函数
 * @param len   所需的参数个数,默认为原函数的形参个数
 */
function curry(fn,len = fn.length) {
 return _curry.call(this,fn,len)
}

/**
 * 中转函数
 * @param fn    待柯里化的原函数
 * @param len   所需的参数个数
 * @param args  已接收的参数列表
 */
function _curry(fn,len,...args) {
    return function (...params) {
         let _args = [...args,...params];
         if(_args.length >= len){
             return fn.apply(this,_args);
         }else{
          return _curry.call(this,fn,len,..._args)
         }
    }
}