Getters & setters


#1

Hi guys,

I’m a bit confused as to why in a function we would want to perform a console.log then return the value of a variable.
I can understand why we would want to return a value, then console.log it, but not the other way round.

Please can you advise?

I’ve pasted below a block of code from the getters & setters part illustrating this.

Thanks
Sophie


let restaurant = {
_name: ‘Italian Bistro’,
_seatingCapacity: 120,
_hasDineInSpecial: true,
_entrees: [‘Penne alla Bolognese’, ‘Chicken Cacciatore’, ‘Linguine pesto’],

set seatingCapacity(newCapacity) {
if (typeof newCapacity === ‘number’) {
this._seatingCapacity = newCapacity;
} else {
console.log(Change ${newCapacity} to a number.)
}
},

get seatingCapacity() {
console.log(There are ${this._seatingCapacity} seats at Italian Bistro.);
return this._seatingCapacity;
}
}

restaurant.seatingCapacity = 150;
const seats = restaurant.seatingCapacity;


#2

Quite likely just to demonstrate the logging. I’m no expert but tend to think of getters as just that: they return a value or object. I wouldn’t sweat it. Once you start writing your own code, you will set the rules of how the program will function.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.