-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13-apply.html
41 lines (33 loc) · 1 KB
/
13-apply.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
37
38
39
40
41
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
Function.prototype.myApply = function (obj) {
console.log('obj', obj, arguments);
let _this = obj || window
_this.temp = this // logName.myCall, 则这里的this就是logName函数
if (arguments[1]) {
// 存在入参
let args = Array.from(arguments[1])
_this.temp(...args) // 此时等于obj调用logName函数,函数内的this指向则是obj
} else {
_this.temp()
}
delete _this.temp
}
var person = {
name: 'lxf'
}
function logName() {
console.log(this.name);
}
logName.myApply(person, ['jack', 'mark'])
</script>
</head>
<body>
</body>
</html>