-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction_call.html
75 lines (63 loc) · 1.52 KB
/
function_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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<!DOCTYPE html>
<html>
<head>
<title>Javascript Function Call Methods</title>
</head>
<body>
<h2>Javascript Function Call Methods</h2>
<h3>These four methods are not applicable to arrow functions</h3>
<script>
function show() {
console.log(this);
return 10;
}
// Method 1
// calling unction as a function is called "Function Invocation Pattern FIP" .
// this - Global object always.
var a = show();
console.log(a);
// Method 2
// constuctor invocation pattern CIP
// this - newly created object
var b = new show();
console.log(b);
// Method 3
// method invocation pattern MIP
// this - object before dot
let dog = {
name: 'abc',
age: 8,
canRun: function() {
console.log(this); // this will print the object
}
}
dog.canRun();
// Method 4
// manually passing value of 'this'
// indirectly call the function, Indirect innovation pattern
// apply, bind, call
let animal = {
country: 'India'
};
let tiger = {
name: 'abc',
age: 8,
canRun: function() {
console.log(this);
}
}
tiger.canRun.call(animal);
tiger.canRun.apply(animal);
let lion = {
name: 'abc',
age: 8,
canRun: function(param) {
console.log(this);
console.log(param);
}
}
lion.canRun.call(animal, 'hi');
lion.canRun.apply(animal, ['how']);
</script>
</body>
</html>