JavaScript dosen't have any form of implicit this
like some other languages do, to access a property on an object you have to explicitly refer to the object (unless you use the deprecated with
statement).
In your example, you'd use the name of the constant:
export const initialState = {
name: '',
acceptable: false,
identified: false,
variation: false,
variationText: () => {
return initialState.name.toUpperCase()
// ^^^^^^^^^^^^^
}
}
Side note: variationText
is not a method in your code, it's an arrow function bound to a property. This is important because if it were a method, in addition to using initialState
as shown above, you could probably (depending on how the method were called) use this
. But you can't with an arrow function assigned to a property, because arrow functions close over this
, they don't get them set by how the function is called. (More here.)
So if you made it a method, and if the method were called in the usual way, you could use this
:
export const initialState = {
name: '',
acceptable: false,
identified: false,
variation: false,
variationText() {
// ^^^^^^^^^^^^^^^^^
return this.name.toUpperCase()
// ^^^^^
}
}
But it's possible to call that method with the wrong this
; details here and here.