!function (proto) {
    function getContext(context) {
        context = context || window;
        var type = typeof context;
        if (['number', 'string', 'boolean', 'null'].includes(type)) {
            context = new context.constructor(context);
        }
        return context;
    }
    function call(context, ...args) {
        context = getContext(context);
        context._fn = this;
        let result = context._fn(...args);
        delete context._fn;
        return result;
    }
    function apply(context, args) {
        context = getContext(context);
        context._fn = this;
        let result = context._fn(...args);
        delete context._fn;
        return result;
    }

    function bind(context, ...bindArgs) {
        //  this = getName
        return (...args) => this.call(context, ...bindArgs, ...args);
    }
    proto.call = call;
    proto.apply = apply;
    proto.bind = bind;
}(Function.prototype)
function getName(age, home){
    console.log(this.name, age, home);
}
let obj = {name: 'xiao'}
// getName.call(obj, 10, 'gzhou')



// getName.apply(obj, [11, 'shenzhen'])


let bindFn = getName.bind(obj, 10)
bindFn('北京')

相关文章:

  • 2022-03-08
  • 2021-09-19
  • 2022-01-05
  • 2021-12-24
  • 2021-12-02
  • 2022-12-23
猜你喜欢
  • 2021-09-20
  • 2022-12-23
  • 2021-12-03
  • 2021-12-31
  • 2021-09-21
  • 2021-07-11
  • 2021-08-27
相关资源
相似解决方案