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 binding - the god principle
5+ Window binding occurs when this is left all alone. Orphaned "this" only has the window to depend on
6+ * 2. Implicit binding -
7+ Uses dot notation inside the object to reference an object property
8+ * 3. New Binding -
9+ hey object I'm going to uses the "new" keyword to introduce you two. You're now bound
10+ * 4. Explicit Binding
11+ .call - comma seperated
12+ .apply - arrays
13+ .bind - returns function
14+ wala
815*
916* write out a code example of each explanation above
1017*/
1320
1421// code example for Window Binding
1522
23+ function WindowBind ( bindtype ) {
24+ console . log ( this ) ;
25+ return bindtype ;
26+ }
27+ WindowBind ( "this isn't bound to anything" ) ;
28+
1629// Principle 2
1730
1831// code example for Implicit Binding
32+ const ImplicitObj = {
33+ greeting : "Hello" ,
34+ sayHello : function ( name ) {
35+ console . log ( `${ this . greeting } ${ name } , would you like to play a game?` ) ;
36+ }
37+ } ;
38+
39+ ImplicitObj . sayHello ( "John" ) ;
40+
1941
2042// Principle 3
2143
2244// code example for New Binding
2345
46+ function NewBinding ( name ) {
47+ this . name = name ;
48+ this . playsgame = true ;
49+ this . question = function ( ) {
50+ console . log ( `Will ${ this . name } play the game? ${ this . playsgame } , he will.` ) ;
51+ } ;
52+ }
53+
54+ const John = new NewBinding ( "John" ) ;
55+ John . question ( ) ;
56+
2457// Principle 4
2558
26- // code example for Explicit Binding
59+ // code example for Explicit Binding
60+
61+ let kitty = [ "cat" , "meow" ] ;
62+
63+ function Animal ( species , sound ) {
64+ this . species = species ;
65+ this . sound = sound ;
66+ }
67+
68+ function Dog ( breed ) {
69+ Animal . call ( this , "Dog" , "Woof" ) ;
70+ this . breed = breed ;
71+ }
72+
73+ function Cat ( breed ) {
74+ Animal . apply ( this , kitty ) ;
75+ this . breed = breed ;
76+ }
77+
78+ const Teddy = new Dog ( "Yorkie" ) ;
79+ const Snuggles = new Cat ( "Tabby" ) ;
80+
81+ console . log ( Teddy ) ;
82+ console . log ( Snuggles ) ;
0 commit comments