U
U
Umid2019-05-01 21:34:31
JavaScript
Umid, 2019-05-01 21:34:31

Why not use for...in, for...of, for(airbnb linter)?

Found only this: https://github.com/airbnb/javascript#iterators--nope
Nevertheless, I would like to get a more detailed answer.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
profesor08, 2019-05-01
@profesor08

Because someone has decided so and his voice has a strong weight. You can always disable this rule.
This is due to the fact that in some browsers everything is bad with iterators, and for..in iterates the properties of the prototype in addition, which is somehow clearly explained in the documentation, and it is impossible to rely on hasOwnProperty in some cases (it is not explained why), probably because it can be redefined for an object, but it can also be redefined for Object, and then everything is bad. Or insufficient browser support.
Summarizing, iterate over the properties of an object, there are arrays for iteration. And properties on that and properties that to them to address directly.

A
Anton Shvets, 2019-05-02
@Xuxicheta

I use for..of on an array only when I need to break the loop ahead of schedule through break. In all other cases - array methods. It just reads faster.
Although I should note that in the case of asynchronous actions, the implementation through reduce is perceived more difficult than await in the for loop, and here I consider it acceptable to violate the requirements of the linter.

B
bro-dev, 2019-05-02
@xPomaHx

Because a callback method from an array prototype is easier to test. For me personally, this is not a convincing argument to abandon procedural searches completely.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question