Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda