File tree Expand file tree Collapse file tree 1 file changed +19
-38
lines changed Expand file tree Collapse file tree 1 file changed +19
-38
lines changed Original file line number Diff line number Diff line change 1
- // nested objects
2
- // set variable as property value
3
- // dot notation vs bracket notation
4
-
5
- const age = 30 ;
6
-
7
- let random = "random value" ;
8
- random = "age" ;
9
-
10
- const person = {
11
- name : "john" ,
12
- age : age ,
13
- siblings : [ "anna" , "susan" , "peter" ] ,
14
- greet : function ( name ) {
15
- console . log ( `Hi, I am ${ name } ` ) ;
1
+ // this
2
+ // points to the object which is left of the dot
3
+
4
+ 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 } ` ) ;
16
10
} ,
17
- sayHello ( name ) {
18
- console . log ( `Hello, My name is ${ name } ` ) ;
19
- } ,
20
- job : {
21
- title : "developer" ,
22
- company : {
23
- name : "coding addict" ,
24
- address : "123 main street" ,
25
- } ,
26
- } ,
27
- "random-value" : "random" ,
28
11
} ;
29
12
30
- console . log ( person . job . company . name ) ;
31
-
32
- console . log ( person . work ) ; // undefined
33
-
34
- /* Cannot read properties of undefined (reading 'title') */
35
- // console.log(person.work.title);
36
-
37
- console . log ( person [ "random-value" ] ) ;
38
-
39
- // check in person object for the property with age which is value of "random"
40
- console . log ( person [ random ] ) ;
13
+ 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
+ } ;
41
21
42
- console . log ( person ) ;
22
+ john . fullName ( ) ;
23
+ bob . fullName ( ) ;
You can’t perform that action at this time.
0 commit comments