Skip to content

Commit 5daa736

Browse files
committed
this.js progress
1 parent c1731f1 commit 5daa736

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

‎this.js‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* The for principles of "this";
22
* in your own words. explain the four principle for the "this" keyword below.
33
*
4-
* 1.
5-
* 2.
6-
* 3.
7-
* 4.
4+
* 1. Used in a "first layer" function within the global scope.
5+
* 2. Used with dot notation as an object method.
6+
* 3. Used with the new keyword, as a constructor function;
7+
* 4. Used with .call or .apply.
88
*
99
* write out a code example of each explanation above
1010
*/
@@ -14,14 +14,35 @@ console.log('hello world!');
1414
// Principle 1
1515

1616
// code example for Window Binding
17+
function globalWindow() {
18+
console.log(this)
19+
}
20+
globalWindow();
1721

1822
// Principle 2
19-
23+
const dogObj = {
24+
dogStatus: 'Cute',
25+
reportDog: function() {
26+
console.log(`This dog is ${this.dogStatus}`)
27+
}
28+
};
29+
dogObj.reportDog();
2030
// code example for Implicit Binding
2131

2232
// Principle 3
2333

2434
// code example for New Binding
35+
function DogStatus(cuteness){
36+
this.prefix = 'This dog is ';
37+
this.cuteness = cuteness.level
38+
this.dogCuteness = function() {
39+
console.log(`This dog is ${this.cuteness}`);
40+
console.log(this);
41+
};
42+
}
43+
44+
const uglyDog = new DogStatus({level: 'Ugly'});
45+
uglyDog.dogCuteness();
2546

2647
// Principle 4
2748

0 commit comments

Comments
 (0)