forked from pluralsight/web-dev-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall_apply_bind.js
37 lines (29 loc) · 930 Bytes
/
call_apply_bind.js
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
// Function burrowing methods
let friendName = {
firstName: "Alex",
lastName: "Rox",
};
let studentName = {
firstName: "Bob",
lastName: "Allen",
};
let myName = {
firstName: "Srimal",
lastName: "Priyanga",
printFullName: function (town, year) {
console.log(this.firstName + " " + this.lastName + " " + town + " " + year);
},
};
const printFullName = function (town, year) {
console.log(this.firstName + " " + this.lastName + " " + town + year);
};
// Call to function within the object
myName.printFullName("City", 2012);
// call & apply methods
printFullName.call(friendName, "Maharagama", 2013);
printFullName.apply(studentName, ["Colombo", 2016]); // arguments as array
// bind
myName.printFullName.bind();
const outputFndName = printFullName.bind(friendName, "New York", 2020);
console.log(outputFndName); // this is the function which can invoke later
outputFndName(); // Calling when it needed