Why can't I use Arrow syntax to return in this problem?

Tell us what’s happening:
Describe your issue in detail here.
So I just started ES6 courses in freeCodeCamp today and I studied the simplified function, which allows us to return by using Arrow Syntax like this “=>” but it seems to not allow me here. Or is my code wrong? Please help

  **Your code so far**
// Only change code below this line
class Thermostat {
constructor(fahrenheit) {
  this._fahrenheit = fahrenheit;
}

get temperature() => 5/9 * (_fahrenheit - 32);
}
// Only change code above this line

const thermos = new Thermostat(76); // Setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in Celsius
thermos.temperature = 26;
temp = thermos.temperature; // 26 in Celsius
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36

Challenge: Use getters and setters to Control Access to an Object

Link to the challenge:

Well an arrow method would be:

temperature = () => 5/9 * (_fahrenheit - 32);

But if I remember correctly, getters and setters can’t be arrow functions. Also, I think _fahrenheit will be undefined, you need to prefix if with something (like you did a few lines before.)

1 Like

Oh I see, thank you so much

Arrow syntax is for defining anonymous functions. So what you’ve written is a direct equivalent to

get temperature function () {
  return 5/9 * (_fahrenheit - 32);
}

Which doesn’t make much sense.

Or as another direct equivalent (a class is used to create objects), defining an object property like this

{
  username "DanCouper"
}

Note the missing colon.

You can define class properties, which are properties that are created every time an object is created via a class definition:

class User {
  username = "DanCouper";
}

console.log(new User()) // { username: "DanCouper" }

That will always be the same value, it gets defined when the object is created.

They can be anonymous functions

class User {
  username = "You forgot to assign the username";

  constructor(username) {
    this.username = username;
  }

  printUsername = () => {
    console.log(this.username)
  };
}

(new User()).printUsername()
// "You forgot to assign the username"
(new User("DanCouper")).printUsername()
// "DanCouper"

The issue is that if you use a class property to define methods, then they get created every time a new object is created, which defeats the point of classes somewhat.

If you just define them normally, as methods, they are attached to the object prototype and only get created once, regardless of how many objects get initialised from the class, like

class User {
  username = "You forgot to assign the username";

  constructor(username) {
    this.username = username;
  }

  printUsername() {
    console.log(this.username)
  }
}

(new User()).printUsername()
// "You forgot to assign the username"
(new User("DanCouper")).printUsername()
// "DanCouper"

This is not to say you always avoid using functions defined as class properties, sometimes you want to have the functions [re]created every time the object is created – an example of this is in component-based UI frameworks like React, where you often define event handler functions in one class that you need to pass to another class. In that case, using class properties for the handlers makes sense, those functions need to be unique to a specific instance of a class, not shared between all of them

1 Like