forked from learning-zone/javascript-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall-apply-bind.html
81 lines (70 loc) · 2.17 KB
/
call-apply-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
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
76
77
78
79
80
81
<!DOCTYPE html>
<html>
<head>
<title>call(), apply() and bind() function in JavaScript</title>
</head>
<script>
/***
* The call() method calls a function with a given this value and arguments provided individually.
*
* The apply() method calls a function with a given this value, and arguments provided as an array.
*
* The bind() method creates a new function that, when called, has its this keyword set to the provided value,
* with a given sequence of arguments preceding any provided when the new function is called.
*
* ***/
// ------------------
// Normal function
// ------------------
var person = {
firstName: 'Pradeep',
lastName: 'Kumar',
fullName: function() {
return this.firstName + ' ' + this.lastName;
}
};
console.log("Normal function: "+person.fullName());
// ------------------------
// Using call() function
// ------------------------
var person = {
firstName: 'Pradeep',
lastName: 'Kumar',
fullName: function() {
return this.firstName + ' ' + this.lastName;
}
};
var myObject = {
firstName: 'Information Technology',
lastName: '& Management'
};
console.info("Using call(): "+person.fullName.call(myObject));
// --------------------
// apply() function
// --------------------
var numbers = [5, 6, 2, 3, 7];
var max = Math.max.apply(null, numbers);
console.log("Using apply(): "+max); // expected output: 7
// Example - 02
var array = ['a', 'b'];
var elements = [0, 1, 2];
array.push.apply(array, elements);
console.info("Using apply(): "+array); // ["a", "b", 0, 1, 2]
// ------------------------
// Using bind() function
// ------------------------
var person = {
firstName: 'Pradeep',
lastName: 'Kumar',
fullName: function() {
return this.firstName + ' ' + this.lastName;
}
};
var myObject = person.fullName;
var getFullName = myObject.bind(person);
console.info("Using bind(): "+getFullName());
</script>
<body>
call(), apply() and bind() function in JavaScript
</body>
</html>