I'm trying to use getters and setters to adjust data initially set with a class constructor. The getter seems to work:
class myClass {
constructor(sth) {
this.data = {
sth,
timestamp: new Date()
}
}
create(createData) {
const newData = Object.assign({}, this.theData, {
/* this getter seems to work ^^^^^^^^^^^^ */
createData
})
console.log(newData) // looks good
// set'ter new data
this.theData(newData)
return this
}
get theData() {
return this.data
}
set theData(newData) {
console.log('setting')
this.data = newData;
}
}
const Instance = new myClass('construct data')
.create({createData: 'some data'})
But this gets the error
zakeyumidu.js:27 Uncaught TypeError: this.theData is not a function
at myClass.create
creating a non-setter
method seems to work as I can just do
setTheData(newData) {
this.data = newData // yep, that works
}
But I get the idea that getters
/setters
are preferred.
Is it okay to set instance data like this within class methods like my example that works? If not, why is my setter not working?
I'm trying to use getters and setters to adjust data initially set with a class constructor. The getter seems to work:
class myClass {
constructor(sth) {
this.data = {
sth,
timestamp: new Date()
}
}
create(createData) {
const newData = Object.assign({}, this.theData, {
/* this getter seems to work ^^^^^^^^^^^^ */
createData
})
console.log(newData) // looks good
// set'ter new data
this.theData(newData)
return this
}
get theData() {
return this.data
}
set theData(newData) {
console.log('setting')
this.data = newData;
}
}
const Instance = new myClass('construct data')
.create({createData: 'some data'})
But this gets the error
zakeyumidu.js:27 Uncaught TypeError: this.theData is not a function
at myClass.create
creating a non-setter
method seems to work as I can just do
setTheData(newData) {
this.data = newData // yep, that works
}
But I get the idea that getters
/setters
are preferred.
Is it okay to set instance data like this within class methods like my example that works? If not, why is my setter not working?
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 14, 2018 at 5:42 12527481252748 15.4k34 gold badges117 silver badges242 bronze badges 1- is it pile time error ? – Pranay Rana Commented Mar 14, 2018 at 5:46
2 Answers
Reset to default 4Instead of this.theData(newData)
you should write this.theData = newData
class myClass {
constructor(sth) {
this.data = {
sth,
timestamp: new Date()
}
}
create(createData) {
const newData = Object.assign({}, this.theData, {
/* this getter seems to work ^^^^^^^^^^^^ */
createData
})
console.log(newData) // looks good
// set'ter new data
this.theData = newData;
return this
}
get theData() {
return this.data
}
set theData(newData) {
console.log('setting')
this.data = newData;
}
}
const Instance = new myClass('construct data')
.create({createData: 'some data'})
this.theData(newData)
would be accessing theData
's getter. Since .data
is not a function at the time that line executes (or ever), that is why you are getting that error.
To fix the problem, you should actually use the setter:
this.theData = newData;