//手写一个mybind,必须放到Function.prototype中,这样函数才能掉用呀
Function.prototype.mybind = function (...args) {
//获取当前的this
let slef = this
//获取第一个参数
let context = args.shift()
//返回的是一个函数
return function (...args1) {
//拼接参数,并用apply来实现
slef.apply(context, [...args, ...args1])
}
}
let obj = {
name: 'xiaoming'
}
//我在node环境下定义的,定义成全局,为了使其this能获取name
name = 'xiaowang'
let fn = function () {
console.log(this);
console.log(this.name);
}
fn(name) //global,xiaoming
fn.mybind(obj)()//obj,xiaowang
手写一个bind函数,利用扩展运算符之简易版
最新推荐文章于 2022-04-11 11:14:23 发布