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

Carl J
  • 1

1 Answers1

1

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:

But seriously just use the arrow function :-)

Bergi
  • 630,263
  • 148
  • 957
  • 1,375