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 when the function this is invoked without any other definitions of it.
5+ * 2. Implicit Binding: Invoking a function with this such as this.function
6+ * 3. Explicit Binding: when methods like .call(), .apply() and bind() are used on a function
7+ * 4. New Binding: this will require us to build _proto_ tree in order access different variables from constructors
88*
99* write out a code example of each explanation above
1010*/
1111
1212// Principle 1
1313
1414// code example for Window Binding
15-
15+ console . log ( this ) ;
1616// Principle 2
1717
1818// code example for Implicit Binding
1919
20+ const SomeObject = function ( ) {
21+ this . name = 'someObjName' ;
22+ } ;
23+
24+ SomeObject . prototype . someFunction = function ( anotherName ) {
25+ console . log ( `${ this . name } is ${ anotherName } ` ) ;
26+ } ;
27+
28+ let csgo = new SomeObject ( ) ;
29+ csgo . someFunction ( 'Counter-Strike Global Offensive' ) ;
2030// Principle 3
2131
2232// code example for New Binding
23-
33+ Humanoid . prototype = Object . call ( CharacterStats . prototype ) ;
34+ function Humanoid ( humanAttributes ) {
35+ this . faction = humanAttributes . faction ;
36+ }
37+
38+ CharacterStats . prototype = Object . call ( Humanoid . prototype )
39+ function CharacterStats ( charAttributes ) {
40+ this . hp = charAttributes . hp ;
41+ }
42+ // this will need to be called in order to access variables throughout different constructors
2443// Principle 4
2544
26- // code example for Explicit Binding
45+ // code example for Explicit Binding
46+ let person = { name : 'Oleks' , countryFrom : ' from Ukraine' } ;
47+ SomeObject . prototype . someFunction . call ( person , person . countryFrom ) ;
0 commit comments