Repository navigation
Expand file tree
/
Copy paththis.js
More file actions
71 lines (52 loc) · 1.8 KB
/
Copy paththis.js
File metadata and controls
71 lines (52 loc) · 1.8 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
63
64
65
66
67
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Window Binding - In global scope, 'this' value is the console Object.
* 2. Implicit Binding - When a function within an object is called with a preceeding dot, the object before is 'this'
* 3. New Binding - When a constructor function is used, 'this' refers to the specific instance of the object that is created and returned by the constructor function.
* 4. Explicit Binding - When using call or apply methods, 'this' is explicitly defined.
*
* write out a code example of each explanation above
*/
console.log('hello world!');
// Principle 1 - WINDOW BINDING
// code example for Window Binding
function washThe(thingToBeWashed) {
console.log(this.thing);
return thingToBeWashed;
};
washThe("windows");
// Principle 2 - IMPLICIT BINDING
// code example for Implicit Binding
const exObj = {
greet: 'Hi',
sayHi: function(name) {
console.log(`${this.greet} my name is ${name}`);
console.log(this);
}
};
exObj.sayHi('Claudio');
// Principle 3 - NEW BINDING
// code example for New Binding
function meanPerson(greeting, meanie, time) {
this.greeting = greeting;
this.meanie = meanie;
this.time = time;
this.speak = function() {
console.log(this.greeting + this.meanie + this.time);
console.log(this);
};
};
let mean = new meanPerson('Hi', 'Bob', 'now')
console.log(mean.time);
// Principle 4 - EXPLICIT BINDING
// code example for Explicit Binding
let me = {
name: 'Claudio',
age: 27
};
let sayName = function(place1, place2, place3) {
console.log(`Hi my name is ${this.name}, Im ${this.age} and Ive been to: ${place1}, ${place2}, ${place3}.`);
};
let places = ['Torrance', 'London', 'Tokyo'];
sayName.call(me, ...places);