Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!