【发布时间】:2021-10-25 22:28:32
【问题描述】:
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)
}
我知道foos.map(foo => foo.getBar())等。
我只想要一个getBar 的版本,它将“this”对象作为其第一个参数。它是否已经存在于某个地方,或者我必须通过unmethodize 或类似的方式创建它?
【问题讨论】:
-
没有任何内置功能可以做到这一点。
标签: javascript