Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
this not binding as expected

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"
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you remove the first console.log, it works for me :

<script type="text/javascript">
const foo = (dispatch) => {
    return {
        name: "Personal Files",
        onSelect: function() {
            dispatch({
                type: "Toto",
                partitionName: this.name
            })
        }
    }
}
foo(({type, partitionName})=>console.log(partitionName)).onSelect()
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!