forked from bloominstituteoftechnology/JavaScript-III
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
62 lines (43 loc) · 1.19 KB
/
this.js
File metadata and controls
62 lines (43 loc) · 1.19 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
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. When in global scope this will bind the window/gobal.
* 2. Implicit binding, when a method is called the object behind the period is this.
* 3. New binding, when a constructor function is called this refers to specific instance of object that is created.
* 4. Explicit binding, when call, apply, or bind is used this is explicitly defined.
*
* write out a code example of each explanation above
*/
// Principle 1
// code example for Window Binding
console.log(this.Document);
// Principle 2
// code example for Implicit Binding
const person = {
name: "Daniel",
speak: function() {
console.log(`My name is ${this.name}`);
}
}
person.speak();
// Principle 3
// code example for New Binding
function Person(a) {
this.name = a.name
};
Person.prototype.speak = function() {
console.log(`My name is ${this.name}`);
};
const person1 = new Person({
name: "Daniel"
});
person1.speak();
// Principle 4
// code example for Explicit Binding
function speak() {
console.log(`My name is ${this.name}`);
}
const dan = {
name: "Daniel"
}
speak.call(dan);