-
Notifications
You must be signed in to change notification settings - Fork 0
/
8 script.js
204 lines (166 loc) · 5.51 KB
/
8 script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! Examples:
//! 1.Template Literals
// var a = 10;
// var b = 20;
// var c = a+b
// console.log(`the value of "a","b" and "c" is: ${a} ${b} ${c}`) //the value of "a","b" and "c" is: 10 20 30
// console.log("the value of a ,b and c is "+a,b,c) //the value of a ,b and c is 10 20 30
// *************************************************************************************************************************
//! 2.this keyword
// const car1 ={
// Brand: "Hyundai",
// model: "i20",
// color: "blue",
// year: "2023",
// drive: function(){
// console.log(`you drive the ${this.Brand}`)
// }
// }
// const car2 ={
// Brand : "Volvo",
// model: "x90",
// color: "black",
// year: "2023",
// drive: function(){
// console.log(`you drive the ${this.Brand}`)
// }
// }
// car1.drive(); //you drive the Hyundai
// car2.drive(); //you drive the Volvo
// *************************************************************************************************************************
//! 3. Class using method
// class Car{
// drive(){
// console.log("Driving the car")
// }
// brake(){
// console.log("applying the brake")
// }
// }
// we are creating a car object using car class
// let car1 = new Car() // let objectname = new classname()
// car1.drive(); //Driving the car
// car1.brake(); //applying the brake
// we can resue the class n number of times
// let car2 = new Car() // let car = new classname()
// car2.drive(); //Driving the car
// car2.brake(); //applying the brake
// *************************************************************************************************************************
//! 4. Class using constructor(properties)
// class Car{
// constructor(brand,model,color,year){
// this.brand=brand;
// this.model=model;
// this.color=color;
// this.year=year;
// }
// drive(){
// console.log("Driving the car")
// }
// brake(){
// console.log("applying the brake")
// }
// }
// let car1 = new Car("Hyundai","i20","red",2023)
// let car2 = new Car("Volvo","X90","black",2022)
// let car3 = new Car("Nexa","Baleno","blue",2023)
// let car4 = new Car("Mahindra","Thar","white",2022)
// console.log(`All the car colors :
// "car1" :${car1.color}
// "car2": ${car2.color}
// "car3": ${car3.color}
// "car4": ${car4.color}`) // All the car colors :
// "car1" :red
// "car2": black
// "car3": blue
// "car4": white
// console.log(car1);
// console.log(car1.name);
// console.log(car1.model);
// console.log(car1.color);
// console.log(car1.year); // Car { brand: 'Hyundai', model: 'i20', color: 'red', year: 2023 }
// console.log(car2);
// console.log(car2.brand);
// console.log(car2.model);
// console.log(car2.color);
// console.log(car2.year); // Car { brand: 'Volvo', model: 'X90', color: 'black', year: 2022 }
// console.log(car3);
// console.log(car3.brand);
// console.log(car3.model);
// console.log(car3.color);
// console.log(car3.year); // Car { brand: 'Nexa', model: 'Baleno', color: 'blue', year: 2023 }
// console.log(car4);
// console.log(car4.brand);
// console.log(car4.model);
// console.log(car4.color);
// console.log(car4.year); // Car { brand: 'Mahindra', model: 'Thar', color: 'white', year: 2022 }
// *************************************************************************************************************************
//! 5.getter and setters
// var users = {
// names: "Bob" //get
// }
// users.names ="Alice" //set
// console.log(users.names); // Alice
// *********** **************** *************** *********
// var users={
// get name(){
// console.log("getting a name")
// return this._name
// },
// set name(n){
// console.log("setting a name"+" "+n)
// this._name =n
// }
// }
// users.name ="Alice"
// console.log(users.name);
// ********* 1.Set the value and 2.Get the value ********************
// So output "setting a name" Alice
// "getting a name"
// Alice
// *************************************************************************************************************************
//! Real time Example1 for Class:
// class Car{
// constructor(power){
// this._power = power;
// }
// get power(){
// return `${this._power}hp` // 400hp
// }
// set power(n){
// retrun `${this._power=n}` // 400
// }
// }
// let car1 = new Car(400);
// console.log(car1.power)
// *************************************************************************************************************************
//! Real time Example2 for Class:
// class Car{
// constructor(power){
// this._gas=25;
// this._power = power;
// }
// get power(){
// console.log(`${this._power}hp`) // 400hp
// }
// get gas(){
// return `${this._gas}l (${this._gas /50*100}%)`;
// }
// set gas(value){
// if(value>50){
// value = 50;
// }
// else if (value<0){
// value=0;
// }
// this._gas=value
// }
// set power(n){
// console.log(`${this._power=n}`) // 400
// }
// }
// let car1 = new Car(400);
// car1.gas=100;
// console.log(car1.power)
// console.log(car1.gas)
// *************************************************************************************************************************