1- /*Knowledge about Object*/
1+ /* Knowledge about Object */
22"use strict" ;
3- /*How to create an empty Object ?*/
3+
4+ /* How to create an empty Object ? */
45let anObject ;
56
6- // same
7+ // same
78anObject = Object . create ( Object ) ;
9+ anObject = Object ( ) ;
810anObject = { } ;
911
1012// without prototype
11- anObject = Object . create ( null ) ;
13+ anObject = Object . create ( null ) ;
14+
15+ // creating with prefilled properties
16+ anObject = {
17+ "key1" : "value1" ,
18+ "key2" : "value2" ,
19+ } ;
20+
21+ // assign simple
22+ anObject [ "key" ] = "value" ;
23+
24+ // assign multiple
25+ Object . assing ( anObject , {
26+ "key1" : "value1" ,
27+ "key2" : "value2"
28+ } ) ;
29+
30+ // get a specific value
31+ anObject [ "key" ] ;
32+
33+ // get Length
34+ Object . keys ( anObject ) . length ;
35+ Object . values ( anObject ) . length ;
36+ Object . entries ( anObject ) . length ;
37+
38+ // iterate over
39+ // keys only
40+ Object . keys ( anObject ) . forEach ( function ( key ) {
41+
42+ } ) ;
43+
44+ // values only
45+ Object . values ( anObject ) . forEach ( function ( value ) {
46+
47+ } ) ;
48+
49+ // keys and values
50+ Object . entries ( anObject ) . forEach ( function ( [ key , value ] ) {
51+
52+ } ) ;
53+
54+ // has a key
55+ anObject . hasOwnProperty ( "key" ) ;
56+
57+ // has safe, works even when anObject has a key "hasOwnProperty"
58+ Object . prototype . hasOwnProperty . call ( anObject , "key" ) ;
59+
60+ // remove a value
61+ anObject [ "key" ] = undefined ;
62+
63+ // completly remove a property (key and value)
64+ delete anObject [ "key" ] ;
65+
66+ // prevent future extensions
67+ Object . preventExtensions ( anObject ) ;
68+ anObject [ "newThing" ] = 2 ; // Error in strict mode
69+
70+ // prevent future extensions and removals
71+ anObject [ "beforeSealing" ] = 10 ;
72+ Object . seal ( anObject ) ;
73+ anObject [ "newThing" ] = 2 ; // Error in strict mode
74+ delete anObject [ "beforeSealing" ] ; // Error in strict mode
75+
76+ // prevent future extensions and removals and mutations
77+ anObject [ "beforeSealing" ] = 10 ;
78+ Object . freeze ( anObject ) ;
79+ anObject [ "newThing" ] = 2 ; // Error in strict mode
80+ delete anObject [ "beforeSealing" ] ; // Error in strict mode
81+ anObject [ "beforeSealing" ] = 11 ; // Error in strict mode
0 commit comments