In my class when I try to make a getter or a setter I get this error
Uncaught TypeError: item.name is not a function
My code is this
class Item {
constructor(n) {this._name = n};
get name() {return this._name};
}
var x = new Item('test')
var newItem = 'New Item: '+x.name()
Getters and Setters will create a pseudo-property with their respective name for the object created by your Class. Thus you can control them without calling them like a function. For Getters:
class Item {
constructor(n) {this._name = n};
get name() {return this._name};
}
var x = new Item('test')
var newItem = 'New Item: '+x.name
console.log(newItem);
And for Setters:
class Item {
constructor(n) {this._name = n};
get name() {return this._name};
set name(n) {this._name = n};
}
var x = new Item('test')
x.name = "foo";
var newItem = 'New Item: '+x.name
console.log(newItem);
Read more about this at MDN's guide