- 1. Understanding and implementation of partial functions.
Concept: understand the meaning of partial function, fixed one or more parameters of the function can be divided into the following steps from the implementation: First, we generally do partial function with placeholders, which means that the parameters of fixed some specific positions cycle to receive the array of parameters, less than the length of FN, placeholder on push. Return a function in which we define an initial variable index = 0 and loop through the arg, If arg[I] === placeholder index++ arg[I] = newArg[index++] return fn.apply(this,arg)
- 2. The realization and understanding of function Currization.
Convert multiple arguments to a single argument through a closure, call recursively, and return a function. Define a function, return the function, and accept arguments in the function. 3. Determine whether the length of the argument passed is equal to the length of the function. Otherwise, return a new function and accept new arguments. 6. Recursively call the function defined in return, passing in parameters.
- 3, the array to several methods, and statistics of the number of string characters appear.
- 4, array intersection difference method.
- 5, the understanding of the Promise and some implementation, then, catch, resolve, reject, finally, all, race
- 6. Understanding the implementation of new.
1. Understanding and implementation of partial functions.
/* function add (a,b){ return a + b + this.value } var value = 'window' // var fn1 = add.bind(null, 1); // 2window var fn1 = partial(add,1) // 2obj var obj = { value: 'obj', fn1:fn1 } console.log(obj.fn1(1)) function partial(fn){ var arg = [].slice.call(arguments,1); return function(){ var newArg = arg.concat([].slice.call(arguments)); return fn.apply(this, newArg) } } */ // console.log({}.toString.call(Symbol('1')).slice(8, // ES5 /* function partail(fn){var args = [].slice.call(arguments,1); for(let i=args.length; i<fn.length; i++){ args.push('_') } return function(){ var newArg = [].slice.call(arguments); var index = 0; for(let i = 0; i<args.length; i++){ if(args[i] === '_'){ args[i] = newArg[index++] } } return fn.apply(this, Function add(a,b,c){return a+b+c} var fn1 = partail(add,'_',1,'_') console.log(fn1(2,1)) */ // ES6 partial function /* const partail = (fn,... args)=>{ let startNum = 0 return (... newArg)=>{ newArg.forEach(item=>{ let index = args.findIndex(res=>res==='_') if (index<0) return args[index] = item startNum++ }) if(startNum<newArg.length){ newArg = newArg.slice(startNum,newArg.length) } return fn.apply(this, [the args,... newArg])}} function add (a, b, c) {return a + b + c} var fn1 = partail (add, '_', 1, '_') console. The log (fn1 (2, 3)) * /Copy the code
2. The realization and understanding of function Currization.
/ / add (1) (2) (3) = = = 6 / / add (1, 2, 3) = = = 6 / / add (1, 2) (3) = = = 6 / * const addNum = (a, b, c) = > a + b + c/method/ES6 a const curry = (fn)=>{ if(fn.length === 1) return fn const next = (... arg)=>{ if(arg.length === fn.length){ return fn(... arg) } else{ return (... arg1)=>{ return next(... arg,... arg1) } } } return next }Copy the code
3, the array to several methods, and statistics of the number of string characters appear.
// Several methods of array de-duplication, from simple to difficult. // const arg = [1,2,3,1]; [/ / / / const arg1 = {/ / name: 1, / /}, / / 2, 3, / / '2', / / {/ / name: // const uniArr = (arg)=>{// let temp = []; // for(let i=0; i<arg.length; i++){ // if(temp.indexOf(JSON.stringify(arg[i]))<0){ // temp.push(JSON.stringify(arg[i])) // } // } // return Temp.map (item=> json.parse (item)) //} // The most efficient es6 new Set() // const uniArr = (arg)=>{// return [...new Parse (v)) // const uniArr = (arg)=>{Set(arg. Map (v=> json.stringify (v)))].map(v=> json.parse (v)) // const uniArr = (arg)=>{ // const temp = {}; // for(v of arg){ // if(! Temp. HasOwnProperty (json.stringify (v)) {// temp[v] = v //} // / return object. values(temp) //} // Use reduce method // const uniArr = (arr)=>{ // return arr.reduce((total,item)=>{ // if(! total.includes(JSON.stringify(item))){ // return total.concat(JSON.stringify(item)) // }else{ // return Total.map (v=> json.parse (v)) //} //},[]) //} // console.log(uniArr(arg1)) // Extends statistics on the number of occurrences of a character in a string // Takes the first place of the highest occurrences // const STR = 'I Love China, go for it! ' // const count = (arg)=>{ // let tempObj = arg.split('').reduce((total,current)=>{ // total[current] ? total[current]++: total[current] = 1 // return total // },{}) // let tempArr = [] // for(i in tempObj){ // tempArr.push({key: i,value:tempObj[i]}) // } // const sortArr = tempArr.sort((a,b)=>-a.value+b.value); // return sortArr //} // console.log(count(STR)) // const arr1 = [1,2,3,{name:1},'22']; // const arr2 = [2,{name:1},'2']; /* const sameArr = (arg1,arg2)=>{ let temp = [] arg1.forEach(item=>{ If (arg2.map(v=> json.stringify (v)).includes(json.stringify (item))){temp.push(item)}}) return temp} */ // can // const sameArr = (arg1,arg2)=>{ // return arg1.filter(item=>arg2.map(v=>JSON.stringify(v)).includes(JSON.stringify(item))) // } // Const sameArr = (arg1,arg2)=>{// return arg1.filter(item=> new Set(arg2.map(v=>JSON.stringify(v))).has(JSON.stringify(item))) // } // console.log(sameArr(arr1,arr2))Copy the code
4. Realization understanding of array expansion Flat
/ / const arr = [1, [2], [[3]], [[[4]]]]. / / the console log (arr. Flat (Infinity)) / / the practice of es5 recursive / * const myFlat = (arg) = > {let temp = [] if(arg.length<1) return for(let i=0; i<arg.length; i++){ if(Array.isArray(arg[i])){ temp = temp.concat(myFlat(arg[i])) }else{ temp.push(arg[i]) } } return temp } // Some const myFlat = (arg)=>{while(arg.some(item=> array.isarray (item))){arg = myFlat(arg).concat(... item) } return arg } console.log(myFlat(arr)) */Copy the code
5, the understanding of the Promise, according to the specification to write one
/* class MyPromise{ constructor(executor){ this.status = 'pending' this._value = undefined this.fulfilledCallbacks = [] this.rejectedCallbacks = [] let resolve = val =>{ const next = ()=>{ if(this.status ! =='pending') return this.status = 'fulfilled' this._value = val while(this.fulfilledCallbacks.length){ const callback = this.fulfilledCallbacks.shift() callback(val) } } setTimeout(next) } let reject = val=>{ const next = ()=>{ if(this.status! =='pending') return this.status = 'rejected' this._value = val while(this.rejectedCallbacks.length){ const callback = this.rejectedCallbacks.shift() callback(val) } } setTimeout(next) } try{ executor(resolve,reject) }catch(err){ reject(err) } } then(onFulfilled,onRejected){ typeof onFulfilled ! =='function' ? onFulfilled =val=>val: null typeof onRejected ! =='function' ? onRejected =err=>err: This will be a big pity (); null return MyPromise((resolve,reject)=>{// Return a Promise let. Try {x instanceof MyPromise? x.then(resolve,reject) : Resolve (x) // Check whether a returned promise is a common value}catch(err){reject(err)}} let rejectedFn = val =>{let x = onRejected(val) try{x instanceof MyPromise ? x.then(resolve,reject): resolve(x) }catch(err){ reject(err) } } if(this.status === 'pending'){ this.fulfilledCallbacks.push(fulfilledFn) this.rejectedCallbacks.push(rejectedFn) }else if(this.status === 'fulfilled'){ fulfilledFn(this._value) }else { rejectedFn(this._value) } }) } catch(reject){ return this.then(undefined,reject) } resolve(value){ if(value instanceof MyPromise ){ return value }else{ return new MyPromise(resolve=>resolve(value)) } } reject(value){ if(value instanceof MyPromise ){ return value }else{ return new MyPromise(reject=>reject(value)) } } finally(callback){ return this.then( value=>Promise.resolve(callback()).then(()=>value), Error => promise.resolve (callback()).then(()=>{throw error}))}} * If there is reject, only reject is returned, Const myALlPromise = promiseArr =>{let index = 0 let result = [] return new MyPromise((resolve,reject)=>{ promiseArr.forEach((element,i) => { MyPromise.resolve(element).then( val=>{ index++ result[i] = val if(index===promiseArr.length){ resolve(result) } }, err =>{ reject(err) } ) }); }) } const myRacePromise = promiseArr =>{ return new MyPromise((resolve,reject)=>{ promiseArr.forEach(item=>{ MyPromise.resolve(item).then( value=>{ resolve(value) }, error=>{ reject(error) } ) }) }) } */ // myALlPromise([p3,p2,p1]).then(item=>{ // console.log(item) // }).catch(err=>{ // console.log(err) // })Copy the code
6. New’s understanding
const Person = function(name,age){ this.name = name this.age = age return this } // const newObj =new Person('angular',18) // console.log(new Object) const _new = (fn,... arg)=>{ const obj = new Object() obj.__proto__ = fn.prototype const result = fn.apply(obj,arg) if(typeof result ==='object' || typeof result ==='function'){ return result }else{ return obj } } const newObj = _new(Person,'angular',18) console.log(newObj)Copy the code