Repository navigation
Expand file tree
/
Copy paththis.js
More file actions
59 lines (43 loc) · 1.44 KB
/
Copy paththis.js
File metadata and controls
59 lines (43 loc) · 1.44 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
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Global Object Binding - "this" binds to the global object window if it is not given
an object to refer to.
* 2. Implicit Binding - "this" is bound to an of an object a method is called on.
The method does something to its object using "this", which is in the object's scope.
* 3. New Binding - "this" is bound to an instance of an object created by a constructor function.
* 4. Explicit Binding - "this" is directly bound to a specific object using the call or apply methods.
*
* 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 robot = {
status : 'undergoing upgrades',
speak : function () {
console.log(`I am currently ${this.status}`);
}
};
robot.speak(); // 'I am currently undergoing upgrades'
// Principle 3
// code example for New Binding
function Car(wheels, engines, seats) {
this.wheels = wheels;
this.engines = engines;
this.seats = seats;
this.sound = function () {
console.log(sound);
};
}
console.log(new Car(4, 1, 5, 'vroom')); // {wheels: 4, engines: 1, seats: 5, sound: 'vroom'}
// Principle 4
// code example for Explicit Binding
const obj = {fruit: 'banana'};
function identifyThis() {
console.log(this.fruit);
}
identifyThis.call(obj); // 'banana'