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

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

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 Relatório

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 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