functionsum(a, b, c){
console.log(this);return a + b + c
}// 函数挂在Function对象构造函数的原型对象上面Function.prototype.newApply=function(context, args){const _this=this
context.fn = _this
let result = context.fn(...args)return result
}let obj ={uname:'zhaoji'}let result = sum.newApply(obj,[1,2,3])
console.log(result);
手写bind()实现
functionsum(a, b, c){
console.log(this)return a + b + c
}// 把bind挂在函数对象的构造函数的原型对象上面Function.prototype.newBind=function(context){// 首先获取到当前上下文,即原函数const _this =this// 获取到要修改的上下文thisconst newThis = context
// 返回一个函数returnfunction(...args){// 这个函数返回原函数的执行结果 只是内部的this指向修改 这里利用apply修改 args是原函数接收的实际参数return_this.apply(newThis, args)}}// 把this指向修改到这个对象上面let obj ={uname:'zhaoji'}const bindFn = sum.newBind(obj)let res =bindFn(4,5,6)
function sum (a, b, c) { console.log(this) return a + b + c}// 把bind挂在函数对象的构造函数的原型对象上面Function.prototype.newBind = function (context) { // 首先获取到当前上下文,即原函数 const _this = this // 获取到要修改的上下文this const newThis = context // 返回一个函数 return fun