-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall-apply-bind.js
More file actions
30 lines (25 loc) · 823 Bytes
/
call-apply-bind.js
File metadata and controls
30 lines (25 loc) · 823 Bytes
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
// call , apply , bind
let name1 = {
firstName:'Satya',
lastName: 'Swaroop',
printFullName: function(){
console.log(`${this.firstName} ${this.lastName}`)
}
}
let name2 = {
firstName: 'MS',
lastName: 'Dhoni'
}
// suppose we have a separte method
let printFullBio = function(state,city){
console.log(`${this.firstName} ${this.lastName} from ${state},${city} `)
}
// printFullBio.call(name1,'Odisha')
// printFullBio.call(name2,'Ranchi')
// Same function expression with apply
printFullBio.apply(name1,['Odisha','Talcher'])
printFullBio.apply(name2,['Jharkhand','Ranchi'])
// now with bind
// bind creates a copy of that function so we can call it whenever we want
let printAllDetails = printFullBio.bind(name1,'Odisha','Talcher')
console.log(printAllDetails) //copy of printFullBio()