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

161
Views
Javascript: pasar un método que se puede llamar con un objeto "este"
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) }

Conozco foos.map(foo => foo.getBar()) etc.

Simplemente quiero una versión de getBar que tome el objeto "este" como su primer parámetro. ¿Ya existe en alguna parte, o debo crearlo a través unmethodize o algo así?

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

0

¿Ya existe en alguna parte?

No, tendrás que crearlo tú mismo. El getBar en su clase define solo un método que espera this argumento.

Sin embargo, si no desea utilizar una función de flecha o escribir su propia función de unmethodize , puede lograr esto solo con funciones integradas:

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

Pero en serio solo usa la función de flecha :-)

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!