Previous
Declaring Fields and Methods
JavaScript has a special syntax for making getter methods
class Person { constructor(first, last) { this.first = first; this.last = last; } get fullName() { return `${this.first} ${this.last}`; } } const teacher = new Person('Ali', 'Madooei'); console.log(teacher.fullName); // Ali Madooei
get
Likewise, there is a special syntax for setter methods.
class Person { get firstName() { return this.first; } get lastName() { return this.last; } set fullName(value) { const parts = value.split(" "); this.first = parts[0]; this.last = parts[1]; } } const teacher = new Person(); teacher.fullName = "Ali Madooei"; console.log(teacher.lastName); // Madooei
set
Getters/setters look like fields, act like methods.
Next
Static Fields and Methods