Want to trim each string in an array, e.g., given
x = [\' aa \', \' bb \'];
output
[\'aa\', \'bb\']
My fi
String.prototype.trim.apply is the Function.prototype.apply method without being bound to trim. map will invoke it with the string, the index and the array as arguments and nothing (undefined) for the thisArg - however, apply expects to be called on functions:
var apply = String.prototype.trim.apply;
apply.call(undefined, x[0], 0, x) // TypeError
What you can do is passing the trim function as the context for call:
[' aa ', ' bb '].map(Function.prototype.call, String.prototype.trim)
// ['aa', 'bb']
What happens here is
var call = Function.prototype.call,
trim = String.prototype.trim;
call.call(trim, x[0], 0, x) ≡
trim.call(x[0], 0, x) ≡
x[0].trim(0, x); // the arguments don't matter to trim