# 代码片段

# 手写new

function myNew(fn, ...args) {
  // 判断是否为方法
  if (typeof fn !== 'function') throw '第一个参数必须为方法'

  const tempObj = {};

  // 继承原型
  tempObj.__proto__ = Object.create(fn.prototype)

  const result = fn.apply(tempObj, args);
  
  const isObject = typeof result === 'object' && result != null
  const isFunction = typeof result === 'function'

  return isObject || isFunction ? result : tempObj;
}

# 手写Bind

Function.prototype.myBind = function(context, ...argArray) {
  var fn = this
  context = (context !== null && context !== undefined) ? Object(context): window

  return function(...args) {
    context.fn = fn
    var finalArgs = argArray.concat(args)
    var result = context.fn(...finalArgs)
    delete context.fn

    return result
  }
}

# 手写apply

Function.prototype.myApply = function(context, ...args) {
  var fn = this
  context = (context !== null && context !== undefined) ? Object(context): window

  context.fn = fn
  argArray = argArray || []
  var result = context.fn(...argArray)
  delete context.fn

  return result
}

# 手写call

Function.prototype.myCall = function(context, ...args) {
  var fn = this
  context = (context !== null && context !== undefined) ? Object(context): window

  context.fn = fn
  var result = context.fn(...args)
  delete context.fn

  return result
}

# 实现函数柯里化

const createCurry = (fn, ...args) => {
  const arr = args || [];
  const length = fn.length;

  return (...res) => {
    const newArr = arr.splice(0);
    newArr.push(...res);
    if (res.length > 0 || newArr.length < length) {
      return createCurry.call(this, fn, ...newArr);
    } else {
      return fn.apply(this, newArr);
    }
  }
}
上次更新时间: 11/7/2021, 2:23:15 PM

添加微信

获取阿里云更多优惠

阿里云最新活动