1
+ /* 3. Objects
2
+
3
+ Note: When objects are declared as literals then singletons are not made,
4
+ When objects are declared as constructors then singletons are made.
5
+
6
+ Object through constructor method, */
7
+ Object . create
8
+
9
+ // And now we will discuss about Object literals below,
10
+
11
+ const mySym = Symbol ( "key1" ) // Declaring a Symbol.
12
+
13
+ const jsUser = {
14
+ name : "Ayush" , /* In this name (key) is treated as string ("name") in system & "Ayush"
15
+ is it's value. */
16
+ "full name" : "Ayush Yadav" ,
17
+ [ mySym ] : "myKey1" , // refering that above declared Symbol as objects key using [].
18
+ age : 21 ,
19
+ loctaion : "Azamgarh" ,
20
+ email : "ayushyadav@gmail.com" ,
21
+ isLoggedIn : false ,
22
+ lastLoggedInDays : [ "Monday" , "Tuesday" , "Thursday" ] // Array as value.
23
+ // We can also give objects as values.
24
+ }
25
+ // Accessing Objects,
26
+ console . log ( jsUser . email ) ; // Output: ayushyadav@gmail .com | Not the best way to access.
27
+ console . log ( jsUser [ "email" ] ) ; /* Output: ayushyadav@gmail.com | Using "" cause this key
28
+ (email) is treated as strings behind the screen. */
29
+ console . log ( jsUser [ "full name" ] ) ; /* Output: Ayush Yadav
30
+ Note: It's not possible to access it like this (console.log(jsUser.full name)) */
31
+ console . log ( jsUser [ mySym ] ) ; /* Acessing that symbol used as key in Object.
32
+
33
+ --------------------------------------------------------------------------------------------
34
+ Changing Values, */
35
+ jsUser . email = "ayushyadav@google.com"
36
+ // Freezing values so that it can't be changed,
37
+ // Object.freeze(jsUser)
38
+ jsUser . email = "ayushyadav@microsoft.com"
39
+ console . log ( jsUser ) ; /* value will not change to "ayushyadav@microsoft.com" cause we have
40
+ freezed the object.
41
+
42
+ ---------------------------------------------------------------------------------------------
43
+ Adding Functions in Object, */
44
+ jsUser . greetings = function ( ) {
45
+ console . log ( "Hi JS user" ) ;
46
+ }
47
+
48
+ jsUser . greetings2 = function ( ) {
49
+ console . log ( `Hi JS user, ${ this . name } ` ) ; // Refering name key from the Object.
50
+ }
51
+
52
+ console . log ( jsUser . greetings ( ) ) ; // Output: Hi JS user
53
+ console . log ( jsUser . greetings2 ( ) ) ; // Output: Hi JS user, Ayush
0 commit comments