-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy paththis.js
More file actions
48 lines (39 loc) · 1.23 KB
/
this.js
File metadata and controls
48 lines (39 loc) · 1.23 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
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Window/Global
Basically when a (this) is used without anything in context like (this.meow), it will instead go for the Window Object
which contains pretty much everything in JS. So console logging (this) will show you the values of everything within JS.
* 2. Implicit
When a (this) is actually being used in a context, with a dot (.) indicating it's being used with a (this).
For example (this.meow) would be refering to the (meow) value.
* 3. New Binding
When (this) is being used to create objects, which can be used to make templates.
* 4. Explicit Binding
Using (this) along with .call or .apply.
*
* 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 Cat = {
sound: 'Meow'
sayMeow: function(options) {
console.log(`${this.sound}`);
}
};
// Principle 3
// code example for New Binding
function Dog(stuff) {
this.bark = "woof";
};
const checkers = new Dog {
bark: 'meow'
};
// Principle 4
// code example for Explicit Binding
checkers.bark.call();