ES6 - Use getters and setters to Control Access to an Object

have a same thoughts.who can explain

please also add a console.log(temp) in the last line.
It will help you to understand what you do :slight_smile:

function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Thermostat {
    constructor(temperature) {
      this._temperature = temperature;
    }
    get temperature() {
      //umrechnung 
      return (this._temperature - 32.0) * 5.0 / 9.0 ;
    }
    set temperature(updatedTemp) {
       this._temperature = ((updatedTemp * 9.0) / 5.0) + 32.0;
    }
  }
  /* 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
console.log(temp);

hope that helps.
Cheers Mchoeti

2 Likes

I got the same end result, didn’t calculate the degrees at any point, just was building up the class and getter/setter functionality. And passed the lesson. Got the idea though.

Thanks for your help!

I just realized my problem with this exercise.

Use class keyword to create a Thermostat class. The constructor accepts Fahrenheit temperature.

Now create getter and setter in the class, to obtain the temperature in Celsius scale.

Nowhere is stated that the setter should accept fahrenheit. Only the constructor, which is called when we’re creating the object.

And I was confused with why this solution validated as correct.

function makeClass() {
“use strict”;
/* Alter code below this line */
class Thermostat {
constructor(temp) {
this.temp = 5/9 * (this.temp - 32);
}
get temperature() {
return this.temp;
}

set temperature(temp) {
  this.temp = temp;
}

}
/* Alter code above this line */
return Thermostat;
}

This shouldn’t be right.

Hi ,
I think you code is not correct for a class. It looks that you have a couple of bugs.
The first one is that if you are assigning a value like this.value inside the constructor and you are doing setters /getters this will lead you to an infinite loop.
if you are trying to access to an static property of a class then you need to declare your setters/getters as static (like : static set value(val){ … })
And if you declare your methods as static you can’t use “this” inside the method because the object is not instanciated jet.
So the above code should be rewrite like:


let value = 'my initial value'

class SomeClass {
  constructor() {
      
  }

  static get value() {
      return value
  }

  static set value(val) {
      value = val
   }
}

// And then you can do 
console.log(SomeClass.value) // 'my initial value'
SomeClass.value = 'new value'
console.log(SomeClass.value)  // 'new value'

There is a bug in the code, but it’s not what you think. For some reason value is reading like a protected keyword, and not a method name, and then blowing the stack.

Edit: Figured it out. I named the instance variable to value, but also the method name was value. That’s what was causing the infinite loop.

I fixed the example to clear this bug. updated post

As for the use of static, that is only for when you want to have access to a class method without instantiating it with new. And this is why you can’t use this

1 Like

I see :+1:

but again , what I was pointing on may second paragraph is that on your example you are trying to access to a static property, becuase you are not instanciating the class (look below)

Yep, it seems I copied the same snippet twice. Should be all cleared up now. Nice catch.

1 Like

Hey,

I’ve just had the same issue here. I wonder it’s a bug. Idk.