Anyone used in real life, thisArg?

Someone knows, someone knows, but the methods Array.forEach, map, reduce, filter and so on - is an optional keyword parameter that allows to pass to the function your context.
For example, the description of forEach: https://developer.mozilla.org/en-US/docs/Web/JavaS...

I'm not asking why this is necessary - I understand how it works and probably can even theoretically to imagine a use-case. But it will be a completely contrived and artificial. But practically - can't. Never I would apply it in real life. In contrast to the methods of call/apply/bind, which solve a similar problem and used quite regularly.

Someone yuzal this keyword in real life? Under what circumstances?
June 10th 19 at 15:04
2 answers
June 10th 19 at 15:06
All for the sake of the "magic" of JavaScript.
In ES6 we have such beauty
addressParts.map(str => str.trim());
And so, this magic will look like this:
addressParts.map(Function.prototype.call, String.prototype.trim);

Instead
addressParts.map(function(str) { return String.prototype.trim.apply(str); });

In short a grain of truth there, if you know this chip. But the same konechno ES6 version is much easier to read.
PS cm blog.bloomca.me/2017/11/08/the-most-clever-line-of... https://stackoverflow.com/questions/19293997/javas...
For me it is the third option the second is clearer :)
But generally speaking, it is also overly complicated, and playing with this they don't need to - why do we call, when the method is applied to the same object, which belongs to? Why not just write
addressParts.map(function(str) { return str.trim(); });
- Evie commented on June 10th 19 at 15:09
well what about the magic?
try your version if
var addressParts = [' a',' v ', 7]; - alvina87 commented on June 10th 19 at 15:12
and option
.map(Function.prototype.call, String.prototype.trim);
yet there is an additional complication with brackets, functions, etc., so either that or if you want an anonymous function, then it is better option ES6 map(str => str.trim()), but he also will throw error on no rows. - alvina87 commented on June 10th 19 at 15:15
The idea in principle understood, but it seems to me very questionable for real-life practice. This magic with implicit conversions is a good way to sooner or later shoot yourself in the foot. - Evie commented on June 10th 19 at 15:18
June 10th 19 at 15:08
Probably for the same reason, when using bind, but it's rare. I, for example, was useful to pass to the function (usually using bind) a specific context, and that is probably because he was too lazy to change this to the name of the parameter that I could pass on in return.

Can thisArg invented before appeared arrow functions

Find more questions by tags JavaScript