Skip to content

Commit c6417f5

Browse files
committed
geter seter in js
1 parent 6f5e45c commit c6417f5

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

11_OOP/geterseter.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@
1010
// set setme(value){
1111
// this._brand = value;
1212
// }
13-
// }
13+
// }
1414

1515

1616

1717
// let mycar = new car('ford', 'mustang', 2023);
18+
// mycar._brand = 'new'
19+
// console.
1820

1921
// mycar.readme = 'BMW' //setter
2022

2123
// console.log(mycar.readme) //getter
2224

2325
class Car {
2426
constructor(brand, model, year) {
25-
this._brand = brand; // real storage
26-
this._model = model;
27-
this._year = year;
27+
this.brand = brand; // real storage
28+
this.model = model;
29+
this.year = year;
2830
}
2931

3032
// Getter for model (to read it)
3133
get model() {
32-
return this._model;
34+
return this._model.toUpperCase();
3335
}
3436

3537
// Setter for model (to change it)
@@ -39,7 +41,7 @@ class Car {
3941

4042
// Extra: Get full info about the car
4143
get info() {
42-
return `${this._brand} ${this._model} ${this._year}`;
44+
return `${this.brand} ${this._model} ${this.year}`;
4345
}
4446
}
4547

@@ -50,9 +52,10 @@ let myCar = new Car('Ford', 'Mustang', 2023);
5052
// console.log(myCar.model); // 👉 Mustang
5153

5254
// Change the model using setter
53-
myCar.model = 'Explorer';
55+
// myCar.model = 'Explorer';
5456

5557
// Read the full car info
5658
// console.log(myCar.info); // 👉 Ford Explorer 2023
5759

58-
console.log(myCar)
60+
console.log(myCar)
61+
console.log(myCar.model)

0 commit comments

Comments
 (0)