-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathBindCallApply.js
More file actions
148 lines (120 loc) · 4.21 KB
/
BindCallApply.js
File metadata and controls
148 lines (120 loc) · 4.21 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// // // // let name = "Vishal";
// // // // let lastName = "Sharme"
// // // // let obj = {name , lastName} //=> {name: name, lastName: lastName}
// // // // console.log(obj)
// // // // settimeout sets this = window for the function call //
// // // window.college = "XYZ college"
// // // const employee = {
// // // name: "Vishal",
// // // printName(a, b, c) {
// // // // if we call this function from line no 22 this == employe
// // // // if we call function from line no 28 this==window
// // // console.log("Hey" , 'name= ', this.name, this.college);
// // // console.log('checking,.,,' , this==employee );
// // // console.log(a,b,c)
// // // }
// // // }
// // // // employee.printName(); // . hey vishal normal behaviour
// // // // loosing this context
// // // // setTimeout(employee.printName , 1000); // this => window
// // // // const functionTimeOut = ()=> {
// // // // console.log('inside the setimout first function' ,this==window) // true
// // // // employee.printName();// this == employee
// // // // }
// // // // // wrapped this with one closure
// // // // setTimeout(functionTimeOut, 1000);
// // // // bind
// const printNameRef = employee.printName.bind(employee, 45, 67, 89);
// // in the bind function first is context or this and rest are the parameter to be passed into the function which we are
// // binding in this cas printName
// setTimeout(printNameRef, 1000);
// // console.log('here this is behaving like window object',this==window)
// const employee_111 = {
// name: "Ashish Rajpoot",
// college: "Naughty College",
// nickName: "Naughty",
// printFullName() {
// console.log(this.name);
// },
// printNickName() {
// console.log(this.nickName);
// },
// };
// const employee_23 = {
// name: "Simita",
// college: "VSSD COLLEGE",
// nickName: "simi",
// };
// const employee_333 = {
// name: "Chandan",
// college: "XYZ",
// nickName: "Chandu",
// };
// employee_1.printFullName(); // basic
// employee_1.printNickName();
// const emp2NameRef1 = employee_1.printFullName.bind(employee_23);
// const emp2NameRef2 = employee_1.printNickName.bind(employee_333);
// emp2NameRef1();
// emp2NameRef2();
// const employee_1 = {
// name: "Rahul",
// college: "VSSD COLLEGE",
// nickName: "Gupta",
// printFullName: () => {
// console.log(this.name);
// },
// printNickName: () => {
// console.log(this.nickName);
// },
// };
// const employee_2 = {
// name: "Nikhat",
// college: "VSSD COLLEGE",
// nickName: "Nikhat",
// };
// const employee_3 = {
// name: "KEERTHANA",
// college: "XY112Z",
// nickName: "kEERTI",
// };
// employee_1.printFullName();
// employee_1.printNickName();
// const emp2NameRef = employee_1.printFullName.bind(employee_2);
// const emp2NickNameRef = employee_1.printNickName.bind(employee_2);
// emp2NameRef();
// emp2NickNameRef();
// const employee_1 = {
// name: "Rahul",
// college: "VSSD COLLEGE",
// nickName: "Seth",
// salary: 2000000,
// };
// const employee_2 = {
// name: "Venu",
// college: "VSSD COLLEGE",
// nickName: "Dara",
// salary: 3000000,
// };
// const employee_3 = {
// name: "Rahul",
// college: "XY112Z",
// nickName: "Kumar",
// salary: 4000000,
// };
// function welComeMessage(message, a, b) {
// // const message= 'Hi , ' + this.name + " " + "We are really happy to onboard you in our next billion jouney";
// const message1 = `Hi, ${this.name} ${message}`;
// console.log(message1);
// console.log(a, b);
// }
// function salary(params, params1, params2) {
// console.log(params, params1, params2);
// }
// // welComeMessage.call(employee_1 , 'We are really happy to onboard you in our next billion jouney' , 234, 45)
// // welComeMessage.call(employee_2 , 'We are really happy to onboard you in our next trillion jouney' , 567,789)
// // welComeMessage.call(employee_3 , 'We are really happy to onboard you in our next trillion path' , 678 , 900)
// // // apply => parameter will be passed as a array
// // // call => paramter is passed as comma seperated
// // // apply
// // salary.apply(employee_1, [123 , 18 ,78]);
// // you have one function => context => // {} => this