This question already has an answer here:
I have a class in JavaScript which goes roughly as follows:
function StateManager () {
this.storeDir = { read: (a, b) => { return false } }
this.state = this.storeDir.read('./state.json', 'json') || { state: 'new' }
}
StateManager.prototype.update = () => {
console.log(this.state.state)
}
module.exports = new StateManager()
Assuming this is a file called "StateManager.js", and we have another file in the same folder, I should be able to call it as follows:
let StateManager = require('./StateManager')
console.log(StateManager)
StateManager.update()
And I expect to see the StateManager
object followed by the current state, which because we're using a dummy function will always be "new". However, for some unknown reason, this
within .update()
is an empty object and hence this.state.state
errors out. StateManager
appears to contain a popular StateManager
object, however, so I can't understand why this is so. Is there a way I can access this.state
within the StateManager.prototype.update
function?
via Popey Gilbert
No comments:
Post a Comment