Skip to content

Commit 02a4ba0

Browse files
authored
Merge pull request #12 from SRedCodes/master
pvt classes and child-parent classes examples
2 parents a1a8a80 + fd6ecba commit 02a4ba0

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

JavaScript/classes/js/main.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
33
class Pizza { // a class is just like a template for object , so it can be used to create mutliple objects of same type
44
constructor(pizzaTopping, pizzaSize){ // all values/data pertaining to the object are stored here
55
this.topping = pizzaTopping; // "this" is a substitute for the new object (yet to be created).
6-
this.crust = "thin";
6+
this._crust = "thin";
77
this.size = pizzaSize;
88
}
9+
getCrust(){
10+
console.log(this._crust);
11+
}
12+
setCrust(myCrust){
13+
this._crust=myCrust;
14+
}
915
pizzaFunction() { // this is a function using which operations are performed on data.
1016
console.log(`this is a ${this.size} ${this.topping} ${this.crust} crust pizza.`);
1117
}
1218
}
1319

1420
const ovenStory = new Pizza("pepperoni" , "large"); // this creates an object ovenStory with class template Pizza();
1521
ovenStory.pizzaFunction();
16-
console.log(ovenStory.topping);
22+
ovenStory.setCrust("Cheese Burst");
23+
ovenStory.getCrust();
1724

1825
class SpecialityPizza extends Pizza{
1926

@@ -30,3 +37,30 @@ class SpecialityPizza extends Pizza{
3037
const mySpeciality = new SpecialityPizza("sausage","Large",10);
3138
mySpeciality.slices();
3239

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

Comments
 (0)