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

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

#1

Tell us what’s happening:
I don’t understand classes and constructors in javascript. Please help me.

Your code so far


function makeClass() {
  "use strict";
  /* Alter code below this line */
   class Thermostat {
     constructor(temperature) {
        this.temperature = temperature;
     }
    
        get temper(){
         return 5/9 * (temperature - 32);
        }
        set temper() {
         return F = temperature * 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 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 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


#2

Your getter should be written something like this:

   get writer(){
     return 5/9 * (this.temperature- 32);
   }

And your setter

   set writer(newValue){
     this.temperature = newValue;
   }

#3

Getters and setters are for accessing/mutating an internal private variable. In this case, they should be called temperature, the internal variable should be called something else.


#4

This challenge is teaching two types of functions: Getters & Setters.

  1. Getters: are functions that only return the values of properties of your choice which are set in the constructor.
  2. Setters: are functions that update the properties of your choice which are set in the constructor.

I’m going to break down the following code:

class Book { 
  constructor(author, title) {
     this.author = author;
     this.title = title;
  }
}

In this part you notice a class named Book, containing a function called constructor. that function is called constructor function. its use is to allow creating a new instace of Book class.

Let’s say, I have a book named: David Copperfield by Charles Dickens. to create a new instance of Book for this, I write the following:

const charlesDickens = new Book('Charles Dickens', 'David Copperfield');

I created a new instance of Book and stored it in charlesDickens.

Note:

  1. The this refers to the current class.
  2. The author and title variable will store the values received from author and title arguments of the constructor function.

What if I want to return or print the author and the title of the book (without allowing direct access to the constructor)?

What if I want to allow myself to update the author or the title of the book at a later time (without allowing direct access to the constructor)?

What should I do?

All can be set inside the constructor function, but if I want to restrict access to the constructor and want only a variable or two to be returned, modified, printed…: Getters and Setters functions will be of great help.

I hope this was helpful.

and Happy Coding!


#5

Thank alot, I will review this later


#6

@clevious
Now, I understand that getter is using to return any values property from a constructor. Where as setter is used to change any values property in a constructor property. Now let me try to solve the challenge. Thank you.


#7

@clevious,@DanCouper,@Tchoukoualeu: Thank you very much. I used your information and I did it just fine.


#8

You’re very welcome.

Good luck & Happy Coding!