Function
Instance Methods
call
- 将函数设为
thisArg
的属性 - 调用函数
- 删除函数
function call(thisArg = window, ...args) { // 逐值传参
thisArg.fn = this
thisArg.fn(...args)
delete thisArg.fn
}
Function.prototype.call = call
apply
- 将函数设为
thisArg
的属性 - 调用函数
- 删除函数
function apply(thisArg = window, args) { // 参数数组
thisArg.fn = this
thisArg.fn(...args)
delete thisArg.fn
}
Function.prototype.apply = apply
bind
- 修改函数
this
指向,并返回修改后的函数
function bind(thisArg, ...args) {
let fn = this
return function(...rest) {
fn.call(thisArg, ...args, ...rest)
}
}
Function.prototype.bind = bind