-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.html
36 lines (36 loc) · 895 Bytes
/
call.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button type="button" id="call">call</button>
<script type="text/javascript">
Function.prototype.myCall = function (context) {
var context = context || window
context.fn = this//核心代码 this指向调用者本身 即callfn
var args = [...arguments].slice(1) //去掉第一个参数 即this
var result = context.fn(...args)
// 删除 fn
delete context.fn
return result
}
let callfn=function(a){
console.log(this, a)
}
let a={
name: 'chenpeng'
}
console.log(callfn.myCall(a,1,2,3))
Function.prototype.myCall=function(context){
let context=context||window;
context.fn=this;
let args=[...arguments].slice(1);
let result=context.fn(...args);
delete context.fn;
return result;
}
</script>
</body>
</html>