Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

171
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda