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

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

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