Skip to content

Commit e0a08fa

Browse files
Orlando LopezOrlando Lopez
authored andcommitted
added solutions for this.js
1 parent 2fcdd38 commit e0a08fa

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

‎assignments/this.js‎

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,64 @@
11
/* The for principles of "this";
22
* in your own words. explain the four principle for the "this" keyword below.
33
*
4-
* 1.
5-
* 2.
6-
* 3.
7-
* 4.
4+
* 1. When "this" has not been defined in a local scope it will take the global meaning of "this."
5+
* 2. When we invoke a method via dot notation, the "this" keyword refers to the Object before the dot.
6+
* 3. When creating objects from a constructor function with the keyword "new", the "this" keyword refers to the specific object created and extracted from the constructor function.
7+
* 4. When using ".apply()" or ".call()" we will be explicitly binding "this."
88
*
99
* write out a code example of each explanation above
1010
*/
1111

1212
// Principle 1
1313

1414
// code example for Window Binding
15+
function dayOfTheWeek(date) {
16+
console.log(this)
17+
}
18+
dayOfTheWeek("Wednesday");
19+
1520

1621
// Principle 2
1722

1823
// code example for Implicit Binding
24+
const food = {
25+
name: "Cheeseburger",
26+
ingredients: ["Cheese", " Meat", " Lettuce", " Bacon"],
27+
howToMake: function() {
28+
console.log(`A ${this.name} has ${this.ingredients}`);
29+
}
30+
}
31+
32+
food.howToMake();
1933

2034
// Principle 3
2135

2236
// code example for New Binding
37+
function BestJSFramework(framework) {
38+
this.framework = framework;
39+
this.end = "is the best framework, period!"
40+
this.iAmBest = function() {
41+
console.log(`${this.framework} ${this.end}`)
42+
}
43+
}
44+
45+
const angular = new BestJSFramework("Angular");
46+
const react = new BestJSFramework("React");
47+
48+
angular.iAmBest();
49+
react.iAmBest();
2350

2451
// Principle 4
2552

26-
// code example for Explicit Binding
53+
// code example for Explicit Binding
54+
const city = {
55+
name: "New York City"
56+
}
57+
58+
const neighborhoods = ["Upper West Side", "East Village", "Lower Manhattan"];
59+
60+
function neighborhoodsInCity(neighborhoods) {
61+
console.log(`${neighborhoods} are located in ${this.name}`);
62+
}
63+
64+
neighborhoodsInCity.call(city, neighborhoods);

0 commit comments

Comments
 (0)