-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththisandbind.js
More file actions
127 lines (91 loc) · 2.81 KB
/
Copy paththisandbind.js
File metadata and controls
127 lines (91 loc) · 2.81 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
// In JavaScript, the thing called 'this' is the object
// that 'owns' the JavaScript code
let hotRod = {};
hotRod = {
sound: 'vroooom',
soundOff: function() {
console.log(this.sound);
}
};
hotRod.soundOff();
let soundFunction = hotRod.soundOff; // 'this' loses its context
soundFunction();
// We can bind 'this' to the object hotRod
// This will give soundFunction a 'this' context
let boundSoundFunction = soundFunction.bind(hotRod);
boundSoundFunction();
let person1 = {
name: 'Joe'
};
let person2 = {
name: 'Jane'
};
function logName() {
return this.name; // 'this' has no context. It's not part of an object
}
// We can bind the function to an object
console.log(logName());
console.log(logName.bind(person1)());
console.log(logName.bind(person2)());
let number = {
x: 24,
y: 22
};
let count = function() {
console.log(this.x + this.y); // no 'this' context here
}
count(); // NaN
let boundCount = count.bind(number);
boundCount();
var a = function() {
console.log(this);
}.bind(1); //I am locking in this to the value I passed in which is 1
a(); //this prints out 1
var asim = {
func: a;
}
asim.func(); //this still prints out 1 because I locked it in with bind
/////////////////////////////
// Lecture: Bind
// One thing to note is that, only in the method call, that the this keyword, actually points to that object. But in a regular function call, the this keyword will always point to the global object
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');
var obiFriendly = obi.presentation.bind(obi, 'friendly'); // this is known as carrying. Carrying is a technique where we base a function on another function but with some preset parameters
obiFriendly('evening');
var emilyFormal = obi.presentation.bind(emily, 'formal');
emilyFormal('morning');
var years = [1990, 1965, 1937, 2005, 1998];
function arrayCalc(arr, fn) {
var arrRes = [];
for (var i = 0; i < arr.length; i++) {
arrRes.push(fn(arr[i]));
}
return arrRes;
}
function calculateAge(el) {
return 2019 - el;
}
function isFullAge(limit, el) {
return el >= limit;
}
var ages = arrayCalc(years, calculateAge);
var fullJapan = arrayCalc(ages, isFullAge.bind(this, 20)); // Japan has a fullAge limit of 20
console.log(ages);
console.log(fullJapan);