-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdvJsTask8.js
More file actions
62 lines (49 loc) · 1.15 KB
/
AdvJsTask8.js
File metadata and controls
62 lines (49 loc) · 1.15 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
//call apply and bind
let name={
firstName:"Mohit",
lastName:"Singh"
}
let printFullName=function(hometown,state){
console.log(this.firstName+" "+this.lastName+" from "+hometown+","+state);
}
//function borrowing
//syntax - functionName.call(objectname,function args);
//objectname is the name of the object this should point to
printFullName.call(name,"korba","Chhattisgarh");
let name2={
firstName:"Rohit",
lastName:"Singh"
}
printFullName.call(name2,"korba","Chhattisgarh");
printFullName.apply(name2,["Korba","Chhattisgarh"]);
//bind method
let printMyname=printFullName.bind(name2,["Korba","Chhattisgarh"]);
console.log(printMyname);
printMyname();
let Student={
age:20
}
function printAge(){
console.log(this.age);
}
let result=printAge.bind(Student);
result();
//Currying
//using bind method
let multiply=function(x,y){
console.log(x*y);
}
let multiplyByTwo=multiply.bind(this,2);
multiplyByTwo(3);
let multiplyByThree=multiply.bind(this,3);
multiplyByThree(3);
//using closures
let add=function(x){
return function(y){
console.log(x+y);
}
}
let addTwo=add(2);
addTwo(3);
let addThree=add(3);
addThree(3);