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?
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 :-)