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

194
Vistas
Pass an object's method to a function in Javascript. Is this the correct interpretation of the question?

I was in an interview, and there was this question:

When the method X of an object O is passed to a function Y as a parameter, what happens if X contains a reference to 'this' and gets executed inside Y? Please provide code examples.

Is this code a correct interpretation of the question?

let O = {
  name: 'John',
  age: 30,
  X() {
    console.log(this.name);
  },
};
let generic = O.X();

function Y(param) {
  return param;
}
console.log(Y(generic));

Could you please help me understand and reply to the question, even if just with an example?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The question asks if the function is passed, not if the function is invoked immediately, then passed. They were probably thinking of something like this:

let O = {
  name: 'John',
  age: 30,
  X() {
    console.log(this.name);
  },
};
function callbackInvoker(fn) {
  fn();
}
callbackInvoker(O.X);

As you can see, unless the passed method is bound first, or is an arrow function, its this binding to the O object will have been lost, which is probably what the question was intending to test you about.

about 4 years ago · Juan Pablo Isaza Denunciar

0


let O = {
  name: 'John',
  age: 30,
  X() {
    console.log(this.name);
  },
};
let generic = O.X;

function Y(fn) {
  return fn(); // <-- execute here
}
console.log(Y(generic));

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