UNPKG
code2021-l
Version:
latest (1.0.1)
1.0.1
1.0.0
前端自定义工具
code2021-l
/
src
/
function
/
apply.js
16 lines
(14 loc)
•
487 B
JavaScript
View Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
export
function
apply
(
fn,obj,args
) {
//处理obj是null或者undefind
if
(obj===
undefined
|| obj===
null
){ obj =
window
}
// 给obj添加一个方法,tempFn 方法,this
obj.
tempFn
= fn
// 调用方法,传入参数,得到返回值
const
result = obj.
tempFn
(...args)
// 删除这个方法
delete
obj.
tempFn
// 返回方法的返回值
return
result }