-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.html
33 lines (27 loc) · 844 Bytes
/
bind.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bind()</title>
</head>
<body>
Bind() method
</body>
<script>
function bindFun(a, b) {
console.log(this.message, a, b);
}
const bindObj = { message: "Hello Bind()" };
let invokeLater = bindFun.bind(bindObj, [6, 33], 600);
// this context is fixed (Permenantly set)
invokeLater.call(); // Hello Bind() [6,33] 600
invokeLater.call({}); // Hello Bind() [6,33] 600
invokeLater.call({ message: "Custom message" }); // Hello Bind() [6,33] 600
// 1. Use case
setTimeout(() => {
bindFun();
bindFun.call({ message: "setTimeout" }, 33, 6); // changed this context of bindFun() function
}, 2000);
</script>
</html>