Skip to content

Commit c94b71c

Browse files
Jashele TJashele T
authored andcommitted
reuploaded work with a new branch
1 parent 33d1564 commit c94b71c

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

assignments/this.js

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,77 @@
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. window/global object binding
5+
> the value of `this` will be the window/console object
6+
7+
* 2. implicit binding
8+
> whenever a function is called by a preceding dot, the object before that dot is `this`.
9+
10+
* 3. new binding
11+
> whnever a constructor function is used, `this1 refers to the specific instance of the object that is created & returned by the constructor function.
12+
13+
* 4. explicit binding
14+
> whenever JavaScript's call or apply method is used, this is explicitly defined.
15+
816
*
917
* write out a code example of each explanation above
1018
*/
1119

1220
// Principle 1
1321

1422
// code example for Window Binding
23+
function sayName(name) {
24+
console.log(this);
25+
return name;
26+
}
27+
sayName("Jashele");
28+
1529

1630
// Principle 2
1731

1832
// code example for Implicit Binding
33+
const myObj = {
34+
greeting: 'Hello',
35+
sayHello: function(name) {
36+
console.log(`${this.greeting} my name is ${name}`);
37+
console.log(this);
38+
}
39+
};
40+
myObj.sayHello('Ryan');
41+
1942

2043
// Principle 3
2144

2245
// code example for New Binding
46+
function CordialPerson(greeter){
47+
this.greeting = 'Hello';
48+
this.greeter = greeter;
49+
this.speak = function() {
50+
console.log (this.greeting + this.greeter);
51+
console.log(this);
52+
}
53+
}
54+
const jerry = new CordialPerson ('Newman');
55+
const newman = new CordialPerson ('Jerry');
56+
57+
jerry.speak();
58+
newman.speak();
59+
2360

2461
// Principle 4
2562

26-
// code example for Explicit Binding
63+
// code example for Explicit Binding
64+
function somePerson(greeter){
65+
this.greeting = 'Hello';
66+
this.greeter = greeter;
67+
this.speak = function() {
68+
console.log (this.greeting + this.greeter);
69+
console.log(this);
70+
}
71+
}
72+
const jashele = new somePerson('Tillman');
73+
const tillman = new somePerson('Jashele');
74+
75+
jashele.speak.call(jashele);
76+
tillman.speak.apply(tillman);
77+

0 commit comments

Comments
 (0)