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