Skip to content

Commit b16ec3a

Browse files
committed
adds code example for principle 3 of this binding
1 parent 182dbeb commit b16ec3a

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

‎assignments/this.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,28 @@ const impliciturBinder = {
2626
}
2727
}
2828

29-
impliciturBinder.findFave();
29+
impliciturBinder.findFave(); // shows that "this" in findFave is implciitly bound to impliciturBinder
3030

3131

3232
// Principle 3
3333

3434
// code example for New Binding
35+
function CoolGuy(name, pet, artist) {
36+
this.name = name;
37+
this.pet = pet;
38+
this.artist = artist;
39+
this.hobby = "skateboarding";
40+
this.girlfriend = "Gal Gadot";
41+
this.car = "Lambo";
42+
this.currency = "Dogecoin";
43+
this.describer = function() {
44+
console.log(`I am a cool guy named ${this.name}. I own a ${this.pet} and my favorite artist is ${this.artist}. I enjoy ${this.hobby}. My girlfriend is ${this.girlfriend}. I drive a ${this.car}. I keep my money in ${this.currency}`)
45+
}
46+
}
47+
48+
const Gabe = new CoolGuy("Gabe", "Shih-Tzu", "Picasso");
49+
Gabe.describer() // shows that "this" in describer is newly bound to Gabe instance of CoolGuy.
50+
3551

3652
// Principle 4
3753

0 commit comments

Comments
 (0)