-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall.js
55 lines (43 loc) · 1.21 KB
/
call.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
43
44
45
46
47
48
49
50
51
52
53
54
55
let obj = {
num: 2
};
let addToThis = function(a, b, c) {
// 'this' has no context here
return this.num + a + b + c;
};
// call binds the function to the first object passed in.
// Parameters are passed in subsequent values
console.log(addToThis.call(obj, 1, 2, 3));
let person1 = {
firstName: 'Jack',
lastName: 'Davis'
};
let person2 = {
firstName: 'Mark',
lastName: 'Price'
};
function hello(greeting) {
console.log(`${greeting} ${this.firstName} ${this.lastName}`);
}
hello.call(person1, 'Good Morning');
hello.call(person2, 'How ya doing');
var obi = {
name: 'Obi',
age: 26,
job: 'teacher',
presentation: function(style, timeOfDay)
{
if (style === 'formal') {
console.log('Good ' + timeOfDay + ', Ladies and gentlemen! I\'m ' + this.name + ', I\'m a ' + this.job + ' and I\'m ' + this.age + ' years old.');
} else if (style === 'friendly') {
console.log('Hey! What\'s up? I\'m ' + this.name + ', I\'m a ' + this.job + ' and I\'m ' + this.age + ' years old. Have a nice ' + timeOfDay + '.');
}
}
};
var emily = {
name: 'Emily',
age: 35,
job: 'designer'
};
obi.presentation('formal', 'morning');
obi.presentation.call(emily, 'friendly', 'afternoon');