File tree Expand file tree Collapse file tree 2 files changed +31
-16
lines changed Expand file tree Collapse file tree 2 files changed +31
-16
lines changed Original file line number Diff line number Diff line change 1
- // this
2
- // points to the object which is left of the dot
1
+ /* In Reg Functions (not arrow) "this"
2
+ determined by "HOW"!!! a function is invoked (left of .)
3
+
4
+ defaults to global - window
5
+ arrow functions - pump the breaks
6
+ */
7
+
8
+ // console.log(this);
9
+
10
+ function showThis ( ) {
11
+ console . log ( this ) ;
12
+ }
3
13
4
14
const john = {
5
- firstName : "john" ,
6
- lastName : "anderson" ,
7
- fullName : function ( ) {
8
- console . log ( this ) ;
9
- console . log ( `hello, my name is ${ this . firstName } ${ this . lastName } ` ) ;
10
- } ,
15
+ name : "john" ,
16
+ showThis : showThis ,
11
17
} ;
12
18
13
19
const bob = {
14
- firstName : "peter" ,
15
- lastName : "anderson" ,
16
- fullName : function ( ) {
17
- console . log ( this ) ;
18
- console . log ( `hello, my name is ${ this . firstName } ${ this . lastName } ` ) ;
19
- } ,
20
+ name : "bob" ,
21
+ showThis : showThis ,
20
22
} ;
21
23
22
- john . fullName ( ) ;
23
- bob . fullName ( ) ;
24
+ john . showThis ( ) ;
25
+ bob . showThis ( ) ;
26
+
27
+ showThis ( ) ;
28
+
29
+ const btn1 = document . querySelector ( ".btn-1" ) ;
30
+ const btn2 = document . querySelector ( ".btn-2" ) ;
31
+
32
+ btn1 . addEventListener ( "click" , showThis ) ;
33
+
34
+ btn2 . addEventListener ( "click" , function ( ) {
35
+ showThis ( ) ;
36
+ } ) ;
Original file line number Diff line number Diff line change 12
12
</ head >
13
13
< body >
14
14
< h1 > objects</ h1 >
15
+ < button class ="btn-1 "> click me</ button >
16
+ < button class ="btn-2 "> click me</ button >
15
17
<!-- Link to JavaScript -->
16
18
< script src ="./app.js "> </ script >
17
19
</ body >
You can’t perform that action at this time.
0 commit comments