This function computes f(g(...arg))
in JavaScript.
function compose(f,g){
return function(...arg){
return f.call(this, g.apply(this, args)};
}
}
I know that the first argument to the methods call
and apply
is the invocation context. However, I am not sure what's the significance of the this
keyword in both call
and apply
methods above. It's clearer to me when the invocation context is an object like {x:1}
, and then we use could use this.x
to access the property x
, but I am not clear on the use of this
in the above context. Why do we use this
like that in the above code?
EDIT: the functions are called with these functions:
const sum = (x, y) => x + y;
const square = x => x * x;
compose(square, sum)(2, 3);