call()、bind()、apply()的用法,改变this的指向,区别在于
f.call(obj, arg1, arg2...),
f.bind(obj, arg1, arg2,...)(),
f.apply(obj, [arg1, arg2, .])
- apply
1 2 3 4 5 function bindThis(f, oTarget) {
return
function() {
return
f.apply(oTarget, arguments)
}
}
- bind
1 2 3 function bindThis(f, oTarget) {
return
f.bind(oTarget)
}
- call
1 2 3 4 5 function bindThis(f, oTarget) {
return
function() {
return
f.call(oTarget, ...arguments)
}
}