1
+ class MyClass {
2
+
3
+ constructor ( value1 , value2 ) {
4
+
5
+ this . public_attibute = value1 ;
6
+ this . _privateAttribute = value2 ;
7
+ }
8
+ get_private_attribute ( ) {
9
+ return this . _privateAttribute ;
10
+ }
11
+ set_private_attribute ( value ) {
12
+ this . _privateAttribute = value ;
13
+ }
14
+ get_public_attribute ( ) {
15
+ return this . public_attibute ;
16
+ }
17
+ set_public_attribute ( value ) {
18
+ this . public_attibute = value ;
19
+ }
20
+ }
21
+
22
+ let my_object = new MyClass ( 2 , 3 ) ;
23
+ console . log ( "Initial value private attribute " , my_object . get_private_attribute ( ) ) ;
24
+ console . log ( "Initial value public attribute " , my_object . get_public_attribute ( ) ) ;
25
+ my_object . set_private_attribute ( 8 ) ;
26
+ my_object . set_public_attribute ( 19 ) ;
27
+ console . log ( "Value private attribute " , my_object . get_private_attribute ( ) )
28
+ console . log ( "Value public attribute " , my_object . get_public_attribute ( ) )
29
+
30
+ //Extra exercise
31
+
32
+ class Stack {
33
+
34
+ constructor ( ) {
35
+ this . array = [ ] ;
36
+ }
37
+ push ( element ) {
38
+ this . array . push ( element )
39
+ }
40
+ pop ( ) {
41
+
42
+ if ( this . array . length > 0 ) {
43
+ this . array . pop ( ) ;
44
+ }
45
+ }
46
+ count ( ) {
47
+ return this . array . length ;
48
+ }
49
+ print ( ) {
50
+ console . log ( this . array )
51
+ }
52
+
53
+ }
54
+
55
+ let my_stack = new Stack ( ) ;
56
+ my_stack . push ( "hi" ) ;
57
+ my_stack . push ( 4 ) ;
58
+ console . log ( "Number of elements" , my_stack . count ( ) ) ;
59
+ my_stack . print ( )
60
+ my_stack . pop ( )
61
+ console . log ( "Number of elements" , my_stack . count ( ) ) ;
62
+ my_stack . print ( )
63
+
64
+
65
+ class myQueue {
66
+
67
+ constructor ( ) {
68
+ this . array = [ ]
69
+ }
70
+ push ( element ) {
71
+
72
+ this . array . push ( element ) ;
73
+ }
74
+ pop ( ) {
75
+
76
+ if ( this . array . length > 0 ) {
77
+ this . array . shift ( ) ;
78
+ }
79
+ }
80
+ count ( ) {
81
+ return this . array . length ;
82
+ }
83
+ print ( ) {
84
+ console . log ( this . array ) ;
85
+ }
86
+ }
87
+
88
+ let my_queue = new myQueue ( ) ;
89
+ my_queue . push ( "Hi!" )
90
+ my_queue . push ( "Bye" ) ;
91
+ console . log ( "Number of records: " , my_queue . count ( ) ) ;
92
+ my_queue . print ( )
93
+ my_queue . pop ( )
94
+ console . log ( "Number of records: " , my_queue . count ( ) ) ;
95
+ my_queue . print ( )
0 commit comments