1

If I trim each string in an array,

[' a ',' b','c '].map(i=>i.trim())

It works.

But [' a ',' b','c '].map(''.trim.call) will cause Uncaught TypeError: undefined is not a function.

I thought it should work...?

0

1 Answer 1

2

You need to bind call to String.prototyp.trim

[' a ','  b','c  '].map(''.trim.call.bind(''.trim))

[' a ','  b','c  '].map(Function.prototype.call.bind(''.trim))

Right now you simply get Function.prototype.call and then invoke it with undefined context

const call = Function.prototyp.call

[' a ','  b','c  '].map(call) 

So each step is just call(item, index, array) while call uses this

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer ! The explanation is very clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.