admin管理员组文章数量:1401624
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;
本文标签: javascriptES6 class setter is not recognized as a functionStack Overflow
版权声明:本文标题:javascript - ES6 class setter is not recognized as a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744251526a2597272.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论