Classes and constructors

The code below is the correct solution to the problem, but I honestly don’t comprehend almost anything about these classes and constructors. Can anyone explain to me exactly how this code works, as well as a good way to think about these classes. Because as it stands, it all looks like nonsense to me ad I can’t make heads or tails of any of it. Thanks!

Edit: Thanks for the help! Looks like I’m going to spend a day or so looking through these resources trying to get a better understanding. I was worried that there was something in the challenges that I wasn’t comprehending.

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
  class Thermostat{
    constructor(farenheit){
      this.farenheit = farenheit;
    }
    get temperature(){
      return 5 / 9 * (this.farenheit - 32);
    }
    set temperature(celsius){
      this.farenheit = celsius * 9.0 / 5 + 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 (X11; CrOS aarch64 11151.29.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.49 Safari/537.36.

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

This is a “working” summary that you may find useful. I hope it is clear enough but feel free to PM me if something is not.
https://drive.google.com/open?id=1lP9L8u07By3NQGje_Itt7RsgpfYhacJA

1 Like

Classes and constructors are concepts that come from OOP (Object-Oriented Programming), which JavaScript doesn’t fully support in any iteration (even in ES6).

If it’s OOP that you’re not understanding, this link might help: https://docs.oracle.com/javase/tutorial/java/concepts/index.html

You could also do either or both of these free online courses:


It might be helpful for you to diverge to another language that fully supports OOP to learn the concepts—examples of such languages include Java, C# or C++, Python, Ruby, Scala, or PHP. Or you might as well learn TypeScript, which is popularly used in Angular and Vue.js.

1 Like

This is not the best introduction to what in JS are called classes. The challenge itself is confusing, plus getters and setters are something that isn’t an ES6 thing (and only have quite specific usecases). I’d say do the OO section of the course (and probably ignore this challenge until you’ve done it).

Note JS fully supports OO programming, it’s just a less common style of OO (prototypical rather than classical), so be careful about transferring knowledge across from other languages — it has different tradeoffs to classical OO, and what is best practice there is not necessarily best practice in JS (stuff that works like OO classes is useful but not necessary in JS — even with the class keyword, JS doesn’t really have classes per se, it just has objects)