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
49 lines (40 loc) · 1.1 KB
/
this.js
File metadata and controls
49 lines (40 loc) · 1.1 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
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Used in a "first layer" function within the global scope.
* 2. Used with dot notation as an object method.
* 3. Used with the new keyword, as a constructor function;
* 4. Used with .call or .apply.
*
* write out a code example of each explanation above
*/
console.log('hello world!');
// Principle 1
// code example for Window Binding
function globalWindow() {
console.log(this)
}
globalWindow();
// Principle 2
const dogObj = {
dogStatus: 'Cute',
reportDog: function() {
console.log(`This dog is ${this.dogStatus}`)
}
};
dogObj.reportDog();
// code example for Implicit Binding
// Principle 3
// code example for New Binding
function DogStatus(cuteness){
this.prefix = 'This dog is ';
this.cuteness = cuteness.level
this.dogCuteness = function() {
console.log(`This dog is ${this.cuteness}`);
console.log(this);
};
}
const uglyDog = new DogStatus({level: 'Ugly'});
uglyDog.dogCuteness();
// Principle 4
// code example for Explicit Binding