forked from bloominstituteoftechnology/JavaScript-II-Mini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
61 lines (44 loc) · 1.64 KB
/
this.js
File metadata and controls
61 lines (44 loc) · 1.64 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
/* The four principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. The Window/Global object Binding Principle - When 'this' is used in the global scope, it will refer to the console object.
* 2. Implicit Binding Principle - When a function is called, the 'this' in the function refers to the object that the function is being called on.
* 3. New Binding Principle - When 'this' is used, it refers to the instance of the object that the constructor is creating.
* 4. Explicit Binding Principle - This principle focuses on the ability to override a constructor using the methods (call, apply, and bind) using explicit or precise elements to replace the constructor instances.
*
* write out a code example of each explanation above
*/
console.log('hello world!');
// Principle 1
// code example for Window Binding
console.log(this);
// Principle 2
// code example for Implicit Binding
const cheeseMenu = {
cheese1: chedder,
cheese2: blue,
cheese3: American,
pickCheese: function() {
console.log('I want ' + this.cheese2 + ' cheese with my chicken wings!');
}
}
cheeseMenu.pickCheese();
// Principle 3
// code example for New Binding
const Towels = {
color: this.color,
purchasedFrom: this.purchasedFrom,
length: this.length,
}
let myTowel = new Towel('blue', 'Target', 'long');
// Principle 4
// code example for Explicit Binding
const sandwich = {
slice1: bread,
middle: pb,
slice2: bread,
}
const makeSandwich = () => {
console.log('Making a sandwich requires ' + this.slice1 + ',' + this.middle + ' and' + this.slice2);
}
makeSandwich.apply(sandwich);