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

154
Views
Javascript: Passing a method that is callable with a "this" object
class Foo {
  constructor(bar) {
    this.bar = bar
  }

  getBar() {
    return this.bar
  }
}

function unmethodize(f) {
  return function(object, ...args) {
    return f.apply(object, args)
  }
}

const unmethodizedGetBar = unmethodize(Foo.prototype.getBar)

function test() {
  foos = [new Foo(1), new Foo(2), new Foo(3)]
  return foos.map(unmethodizedGetBar)
}

I know about foos.map(foo => foo.getBar()) etc.

I simply want a version of getBar that takes the "this" object as its first parameter. Does it already exist somewhere, or must I create it via unmethodize or some such?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Does it already exist somewhere?

No, you'll have to create it yourself. The getBar in your class defines only a method that expects a this argument.

If you don't want to use an arrow function or write your own unmethodize function, you can however achieve this with builtins only:

  • const unmethodize = Function.bind.bind(Function.call);
  • foos.map(Function.call.bind(Foo.prototype.getBar))
  • foos.map(Function.call, Foo.prototype.getBar)

But seriously just use the arrow function :-)

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!