call()、bind()、apply()的用法,改变this的指向,区别在于接收的参数
f.call(obj, arg1, arg2…),
f.bind(obj, arg1, arg2,…)(),
f.apply(obj, [arg1, arg2, .])
实例:封装函数 f,使 f 的 this 指向指定的对象
//apply
function bindThis(f, oTarget) {
return function() {
return f.apply(oTarget, arguments)
}
}
//bind
function bindThis(f, oTarget) {
return f.bind(oTarget)
}
//call
function bindThis(f, oTarget) {
return function() {
return f.call(oTarget, ...arguments)
}
}