1.手写深拷贝
function deepClone(obj) {
var newObj
if(obj && typeof obj !== 'object')
{
newObj = obj
}
else if(obj && typeof obj === 'object')
{
newObj = obj instanceof Array ? [] : {}
for(let key in obj)
{
if(obj.hasOwnProperty(key))
{
newObj[key] = deepClone(obj[key])
}
else
{
newObj[key] = obj[key]
}
}
}
return newObj
}
2.call方法
Function.prototype._call = function(context) {
context = (context === null || context === undefined) ? window : Object(context)
context.fn = this
let arg = Array.from(arguments).slice(1)
let result = context.fn(...arg)
delete context.fn
return result
}
3.apply方法
Function.prototype._apply = function(context, arg) {
context = (context === null || context === undefined) ? window : Object(context)
context.fn = this
let result = (arg === null) ? context.fn(...arg) : context.fn()
delete context.fn
return result
}
4.bind方法
Function.prototype._bind = function(context) {
context = (context === null || context === undefined) ? window : Object(context)
context.fn = this
let args = [...arguments].splice(1)
return function() {
let newArgs = [...arguments]
let result = context.fn.apply(context, args.concat(newArgs))
delete context.fn
return result
}
}
5.乞丐版Promise
const PENDING = 'pending'
const RESOLVE = 'resolve'
const REJECT = 'reject'
class Promise{
constructor(fnc){
this.state = PENDING
this.value = undefined
this.reason = undefined
let resolve = (data) => {
if(this.state === PENDING)
{
this.value = data
this.state = RESOLVE
}
}
let reject = (data) => {
if(this.state === PENDING)
{
this.reason = data
this.state = REJECT
}
}
try{
fnc(resolve,reject)
}catch(e){
reject(e)
}
}
then(onfulfilled, onreject){
if(this.state === resolve)
{
onfulfilled(this.value)
}
if(this.state === reject)
{
onreject(this.reason)
}
}
}
module.exports = Promise
6.乞丐版Promise.all
const PromiseAll = (promises) => {
return new Promise((resolve,reject) => {
if(!Array.isArray(promises))
{
throw new TypeError('error')
}
let result = [promises.length]
let count = 0
promises.forEach((promise,index) => [
promise.then((res) => {
result[index] = res
count++
count === promises.length && resolve(result)
}),(err) => {
reject(err)
}
])
})
}
7.防抖
function debounce (fnc,delay) {
let timer = null
return function() {
let context = this
let arg = arguments
clearTimeout(timer)
timer = setTimeout(() => {
fnc.apply(context,arg);
},delay)
}
}
window.addEventListener('resize',debounce(foo,2000))
8.节流
function throttle(fnc, delay){
let timer = null
return function(){
let context = this
let arg = arguments
if(!timer)
{
timer = setTimeout(() => {
fnc.apply(context,arg)
timer = null
},delay)
}
}
}
9.函数柯里化
function curryingFun(fn, length) {
//第一次调用获取函数 fn 参数的长度,后续调用获取 fn 剩余参数的长度
length = length || fn.length;
// curryingFun 包裹之后返回一个新函数,接收参数为 …args
return function (...args) {
// 新函数接收的参数长度是否大于等于 fn 剩余参数需要接收的长度
return args.length >= length
? // 满足要求,执行 fn 函数,传入新函数的参数
fn.apply(this, args)
: // 不满足要求,递归 curryingFun 函数,新的 fn 为 bind 返回的新函数(bind 绑定了…args 参数,未执行),新的 length 为 fn 剩余参数的长度
curryingFun(fn.bind(this, ...args), length - args.length);
};
}