I have an example that appears to me to be identical to the example given in the mozilla docs
const foo = (dispatch) => {
return {
name: "Personal Files",
onSelect: function() {
dispatch({
type: MoveDocActionType.ENTER_PARTITION,
partitionName: this.name
})
}
}
}
console.log(foo(({type, partitionName})=>console.log(partitionName)).onSelect())
// Expected output "Personal Files"
The Mozilla docs example:
const test = {
prop: 42,
func: function() {
return this.prop;
},
};
console.log(test.func());
// expected output: 42
However, during actual execution, this
is undefined in my case.
What am I missing, as this seems to be identical to the docs, so I don't see why I would need to manually call bind or anything like that.
EDIT: Found the answer here
The way I was actually calling the method was closer to this:
const foo = (dispatch) => {
return {
name: "Personal Files",
onSelect: function() {
dispatch({
type: MoveDocActionType.ENTER_PARTITION,
partitionName: this.name
})
}
}
}
const bar = foo(({type, partitionName})=>console.log(partitionName)).onSelect
console.log(bar()
// Expected output "Personal Files"