As a person who has worked mainly with languages ​​with OOP, languages ​​with static typing, it is not quite clear to me what practical use all these troubles have with contexts in JavaScript.

Many tutorials say

You can call a function in the context of another object so that the entire inside of the object will be available to it.

and some kind of forEach is given, where for listing you need to have nothing at all.

However, if you look at the article forEach , then nowhere is there a description that he uses under the hood. Probably, you can find the source code somewhere and see what needs to be used for the iteration, however, isn't this digging in dirty implementation linen?

In other PLs, this is found on the surface in the form of some kind of interfaces.

Well, I also don’t understand why all dances with a loss of context and this is the default behavior and if I need to make the callback work normally, then I have to use the context binding.

  • 2
    This language uses a different programming paradigm. No language is obliged to follow any other languages ​​in one way or another. The question "why this is done, if others are not so" does not make sense. - Mike
  • As for forEach, it's just a convenient method of listing properties. in another language, you would use ordinary for(...;...;...) in its place. About "under the hood", I did not understand what you meant. But I note that in the languages ​​about which you speak, there are simply no built-in data types, at least as similar to JS objects. Accordingly, there are no analogues forEach - Mike
  • The algorithm forEach from Array.prototype described in the specification . The specification also states the following: The forEach function is intentionally generic ; it is not an Array object. Therefore, it can be transferred. - Grundy
  • @Mike each paradigm has its own + and -. What is the + approach that was chosen in js? IMHO, some traditional approach would be nothing worse. - Zabuza
  • one
    @Zabuza It gives an extraordinary flexibility, which in "traditional OOP languages" is unattainable, without the use of any very complex technologies like reflection. In general, just write something less serious on it and say it personally for you, these are pluses or minuses. No one can list the benefits to you, simply because all people have different thinking and the fact that for one is a plus, for another - Mike

0