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
Add a ment  | 

2 Answers 2

Reset to default 4

Instead 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