Promise.resolve
refers to the resolve
function without context object.
You want to call it with the proper context object. This can be done
- by calling it on that context object, as in
v => Promise.resolve(v)
, or
- by creating a bound version of it, as in
Promise.resolve.bind(Promise)
So, this would work:
compose(
console.log,
map(Promise.resolve.bind(Promise))
)([7,8,9]);
Remember that Javascript does not have classes. Functions have no owner. Objects can store functions in their properties, but that does not mean the function is owned by that object.
Another way is setting the context object explicitly, with Function#call
or Function#apply
:
function (v) {
var resolve = Promise.resolve;
return resolve.call(Promise, v);
}
Maybe it's best illustrated by focusing on something other than a method:
function Foo() {
this.bar = {some: "value"};
this.baz = function () { return this.bar; };
}
var f = new Foo();
var b = f.bar;
var z = f.baz;
here b
refers to {some: "value"}
without {some: "value"}
magically "knowing" that f
stores a reference to it. This should be obvious.
The same is true of z
. It stores a function without that function "knowing" that f
also references it. This should be just as obvious, in theory.
Calling z()
will yield different results than calling f.baz()
, even though the called function is the same one. Only the context is different.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…