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

206
Visualizações
How exactly does 'this' keyword assign properties to functions in javascript?

I tried to recreate javascript promise, following this tutorial, this is the complete code from the tutorial:

function myPromise(callback) {

    let resolve = function(value){
        this.resolveCallback(value);
    }
    callback(resolve)

}

// .then
myPromise.prototype.then = function(resolveCallback){
    attachResolveCallback(resolveCallback);
}

const attachResolveCallback = function(resolveCallback){
  this.resolveCallback = resolveCallback;
}

im confused about the part that implements .then():

// .then
myPromise.prototype.then = function(resolveCallback){
    attachResolveCallback(resolveCallback);
}

const attachResolveCallback = function(resolveCallback){
  this.resolveCallback = resolveCallback;
}

Why is const attachResolveCallback needed, instead of just using this keyword in the prototype function?

So I replaced the .then part of the code with:

myPromise.prototype.then = function(resolveCallback){
    this.resolveCallback = resolveCallback;
}

But the code does not work and returns an error:

promise.js:4 Uncaught TypeError: this.resolveCallback is not a function
    at resolve (promise.js:4:14)
    at promise.js:25:9

I suspect this problem is due to usage of 'this' when assigning properties to functions, but im not sure about the specific reason here. Could someone enlighten me on this problem?

about 4 years ago · Juan Pablo Isaza
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