arr.map(String.prototype.trim.call.bind(String.prototype.trim));
call uses this internally, which should point to the trim function to work correctly in this case. Just passing String.prototype.trim.call left the call unconnected with any method, as a result of which this value will point to window .
It works, but when used it is used instead of calling, it throws an error, arr.map (String.prototype.trim.apply.bind (String.prototype.trim));
The problem is that map will pass 2 arguments, an element and an index. Therefore, it calls a call of something like 'String.prototype.trim.apply('test', 0) , which fails because the second argument must be an array.
one more thing ['A', 'B', 'C']. Map (String.prototype.trim.call.bind (String.prototype.toLowerCase)) ;, in this I used trim.call and passed toLowerCase as the context, then why do we need to trim here, why the trim is not called
When using call.bind path that you chose to access the call function becomes irrelevant. The function to be called is called.
If you want to compose functions, you will need a different approach:
var call = Function.prototype.call, trim = call.bind(String.prototype.trim), toLowerCase = call.bind(String.prototype.toLowerCase), trimAndLowerCase = pipelineFrom(trim, toLowerCase); [' TeST '].map(trimAndLowerCase); function pipelineFrom(fn1, fn2) { return function (val) { return fn2(fn1(val)); }; }
However, at this point you will be better off:
arr.map(function (val) { return val.trim().toLowerCase(); });