手写
sum
// 步骤// 1. 利用闭包收集参数// 2. 使用reduce计算结果function addSum(){ let args = [...arguments]; // 收集参数 function collectArgs(){ args = [...args, ...arguments]; return collectArgs; } // 计算结果 collectArgs.valueOf = function(){ return args.reduce((pre, cur) => pre + cur, 0) } return collectArgs;}console.log(addSum(1)(2)(3).valueOf()) // 6
偏函数
// 偏函数是柯里化的子集// 固定一个参数function partial(fn, ...args) { return function(...newArgs) { return fn.apply(this, [...args, newArgs]); }}// 使用function add(num1, num2){ return num1 + +num2 ;}// 普通函数转换为偏函数let addOne = partial(add, 1);console.log(addOne(2)); // 3console.log(addOne(3)); // 4
this输出问题
instanceof
// instanceof 用于检测构造函数的prototype属性是否出现在某个实例对象的原型上function myInstanceof(left, right) { // left 为实例对象 // right 为构造函数 let leftProto = Object.getPrototypeOf(left); let RightProto = right.prototype; while (1){ // 如果找到原型链的尽头还是没跳出,证明不是由该构造函数实例化的对象 if(!leftProto) return false; if(leftProto === RightProto) return true; leftProto = Object.getPrototypeOf(leftProto); }}
create
// Object.create方法用于创建对象// 第一个参数为传入的原型对象,所创建的对象的原型对象会指向传入的原型对象Object.myCreate = function(proto, propertiesObject) { // 参数校验 if(typeof(proto) !== "object"){ throw new Error("Proto is not an object") } function tempFn(){} tempFn.prototype = proto; let tempObj = new tempFn(); if(propertiesObject){ Object.defineProperties(tempObj, propertiesObject); } return tempObj;}
算法
map





每日一题
CV了一份 二分查找
富文本编辑器