forked from bloominstituteoftechnology/JavaScript-II-Mini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
73 lines (55 loc) · 1.88 KB
/
this.js
File metadata and controls
73 lines (55 loc) · 1.88 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
68
69
70
71
72
73
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. window binding binds to the object of the window itself that you are inside of running the code.
* 2. implicet binding is binding to the object that is actually using the this. keyword
* 3. new binding is binding the this. property to the new object being created from the prototype object class or as i call it the object base class.
* 4. Explicit binding is binding from outside the object in question using a function or something else to connect to the object.
*
* write out a code example of each explanation above
*/
// Principle 1
// code example for Window Binding
this.car = 'convertable'; // binds this to the window itself so the windows.car = 'convertible'
// Principle 2
// code example for Implicit Binding
// create your object.
const car = {
//key: value,
color: 'red',
type: 'convertible',
speed: 0,
};
// use a function to connect to the object and allow your this to be use with the object in question.
car.carsSpeed = (speed) => {
return this.speed = 90;
}
console.log(car.carsSpeed());
// Principle 3
// code example for New Binding
let carss = function(color, type, speed) {
//key: value,
this.color = color;
this.type = type;
this.speed = speed;
};
let corvette = new carss('blue', 'sedan', 1000);
console.log(corvette);
// Principle 4
// code example for Explicit Binding
// use .call .bind .apply
// function.bind(thisArg[,arg1[,arg2[,argN]]]
// create your object.
const carsss = {
//key: value,
color: 'red',
type: 'convertible',
speed: 0,
getSpeed: function() {
return this.speed;
}
};
// use a function to connect to the object and allow your this to be use with the object in question.
const newcars = carsss.getSpeed;
const newCar = newcars.bind(carsss);
console.log(newCar());