-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallapplyblind.js
42 lines (33 loc) · 1.62 KB
/
callapplyblind.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
38
39
40
41
42
/** @format */
//Call: The call() method invokes a function with a given this value and arguments provided one by one
var employee1 = { firstName: 'John', lastName: 'Rodson' };
var employee2 = { firstName: 'Jimmy', lastName: 'Baily' };
function invite(greeting1, greeting2) {
console.log(
greeting1 + ' ' + this.firstName + ' ' + this.lastName + ', ' + greeting2,
);
}
invite.call(employee1, 'Hello', 'How are you?'); // Hello John Rodson, How are you?
invite.call(employee2, 'Hello', 'How are you?'); // Hello Jimmy Baily, How are you?
//Apply: Invokes the function with a given this value and allows you to pass in arguments as an array
var employee1 = { firstName: 'John', lastName: 'Rodson' };
var employee2 = { firstName: 'Jimmy', lastName: 'Baily' };
function invite(greeting1, greeting2) {
console.log(
greeting1 + ' ' + this.firstName + ' ' + this.lastName + ', ' + greeting2,
);
}
invite.apply(employee1, ['Hello', 'How are you?']); // Hello John Rodson, How are you?
invite.apply(employee2, ['Hello', 'How are you?']); // Hello Jimmy Baily, How are you?
//bind: returns a new function, allowing you to pass any number of arguments
var employee1 = { firstName: 'John', lastName: 'Rodson' };
var employee2 = { firstName: 'Jimmy', lastName: 'Baily' };
function invite(greeting1, greeting2) {
console.log(
greeting1 + ' ' + this.firstName + ' ' + this.lastName + ', ' + greeting2,
);
}
var inviteEmployee1 = invite.bind(employee1);
var inviteEmployee2 = invite.bind(employee2);
inviteEmployee1('Hello', 'How are you?'); // Hello John Rodson, How are you?
inviteEmployee2('Hello', 'How are you?'); // Hello Jimmy Baily, How are you?