3
3
class Pizza { // a class is just like a template for object , so it can be used to create mutliple objects of same type
4
4
constructor ( pizzaTopping , pizzaSize ) { // all values/data pertaining to the object are stored here
5
5
this . topping = pizzaTopping ; // "this" is a substitute for the new object (yet to be created).
6
- this . crust = "thin" ;
6
+ this . _crust = "thin" ;
7
7
this . size = pizzaSize ;
8
8
}
9
+ getCrust ( ) {
10
+ console . log ( this . _crust ) ;
11
+ }
12
+ setCrust ( myCrust ) {
13
+ this . _crust = myCrust ;
14
+ }
9
15
pizzaFunction ( ) { // this is a function using which operations are performed on data.
10
16
console . log ( `this is a ${ this . size } ${ this . topping } ${ this . crust } crust pizza.` ) ;
11
17
}
12
18
}
13
19
14
20
const ovenStory = new Pizza ( "pepperoni" , "large" ) ; // this creates an object ovenStory with class template Pizza();
15
21
ovenStory . pizzaFunction ( ) ;
16
- console . log ( ovenStory . topping ) ;
22
+ ovenStory . setCrust ( "Cheese Burst" ) ;
23
+ ovenStory . getCrust ( ) ;
17
24
18
25
class SpecialityPizza extends Pizza {
19
26
@@ -30,3 +37,30 @@ class SpecialityPizza extends Pizza{
30
37
const mySpeciality = new SpecialityPizza ( "sausage" , "Large" , 10 ) ;
31
38
mySpeciality . slices ( ) ;
32
39
40
+ class burger {
41
+ sauce = "tomato" ; // is public property cam be accessed outside this class
42
+ #patty; // patty is # private property and cannot be accessed outside this class burger
43
+ #size;
44
+ constructor ( size ) {
45
+ this . size = size ;
46
+ this . #patty = "chicken" ;
47
+ }
48
+ getPatty ( ) {
49
+ console . log ( this . #patty) ;
50
+ }
51
+ setPatty ( myPatty ) {
52
+ this . #patty = myPatty ;
53
+ }
54
+
55
+ order ( ) {
56
+ console . log ( `this is a ${ this . size } size ${ this . #patty} Burger` ) ;
57
+ }
58
+ }
59
+ const myBurger = new burger ( "Whopper" ) ;
60
+ myBurger . order ( ) ;
61
+ myBurger . setPatty ( "goat" ) ;
62
+ myBurger . getPatty ( ) ; //though patty is private we can access it using function.
63
+ myBurger . order ( ) ;
64
+ myBurger . #patty = "turkey" ; // throws error patty is pvt and is not accessible outside burger class.
65
+
66
+
0 commit comments