Use getters and setters to Control Access to an Object I don't understand

Use getters and setters to Control Access to an Object I don't understand
0.0 0

#1

Tell us what’s happening:

I got it right(well, I’m not really sure since this challenge has very few tests) but the problem is that the lesson doesn’t explain anything, anything at all about the syntax of getters and setters. I mean what is their actual syntax(I really don’t seem that I got it)? What is their purpose? Why we can’t just use object.entry = “something” instead of using getters and setters I really don’t understand?
Can anyone please explain these to me? Right now, freecodecamp is really suffering from the new horrible lessons.
Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Thermostat {
    constructor(Fahrenheit) {
      this.Fahrenheit = Fahrenheit;
    }
    get Celsius() {
      return this.Fahrenheit;
    }
    set Celsius(temperature) {
      this.Temperature = 5/9 * (Fahrenheit - 32);
    }
  }
  /* Alter code above this line */
  return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/use-getters-and-setters-to-control-access-to-an-object


#2

Any help here, please?

This might be an important lesson and I really want to learn it before moving on.


#3

Anyone help? …


#4

Because although a class is an object in javascript, it is not an object literal. So you cannot set properties on a class externally. You have to do it through internal methods.

Getters and setters are methods in the class that give you the behavior you want which is

console.log(object.entry) // 2
object.entry = 3
console.log(object.entry) // 3

#5

This module is broken actually. It will pass with a blank class.