A
A
AlphaDMQ2018-01-26 22:59:37
JavaScript
AlphaDMQ, 2018-01-26 22:59:37

What's under the hood of [].forEach()?

Dear Toasters. Please help me with info. Nowhere is this mechanism described normally.
It is very important to understand how forEach works on an array. Namely:
1. Why is it necessary to explicitly pass the context as the second parameter?
2. Where is the callback called from every time?
3. Is the callback a new object each time?
4. How does the method work step by step, in detail?
Please write down to the smallest detail.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2018-01-26
@AlphaDMQ

1. If you don't want it, don't pass it on. It is not needed at all, you can use bind to pass this https://jsfiddle.net/eyc8cmqu/1/
2. I mean where? The callback is called in the same context where forEach
3 is called. It's the same function. . If you are talking about a passed argument, then it is the array element that is passed to the function as the first argument, if it is a primitive, then it is copied to the function by value.
4. Loops from 0 to the original length of the array and executes a callback on each existing element.
It's all written https://developer.mozilla.org/ru/docs/Web/JavaScri...
upd corrected the answer after criticism from the user Anton Spirin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question