Doubts about Airbnb's "Iterators and Generators" eslint rule

  1. did not understand the meaning of that paragraph in why, asking for an answer.
  2. if I want to iterate through the loop, there is a judgment statement that can advance return, break, and continue.
    now that Airbnb"s eslint rules are used, for.of syntax is not recommended, and higher-order functions are recommended, so what if you can"t advance return,? Do you need to iterate through the loop before return?
May.26,2021

  1. first of all, the map () / every () method guarantees the invariance of the array, while the for depends entirely on who wrote the code.
  2. the semantic rules are obvious. You know what you're going to do.
  3. these methods use algorithmic implementations that are relatively fast (perhaps not as fast as your custom implementation but general enough), and the engine itself optimizes it.
  4. of course, using these methods may not be precisely controlled, but they are usually not needed (there are basically methods corresponding to the application scenario).

it's fine upstairs. I don't know why someone clicked against it. I'd like to add some:

  • when I was refactoring a company project, I found that the previous programmer was probably a for..in for..of shuttle. If you read too much this kind of code, you will become very confused, because you don't know what this for loop is doing, that is, the second point mentioned upstairs
  • .
  • sometimes you need to add some Filter logic to the traversal, and then there will be a large number of if..else, in the for loop statement block, which further deepens the confusion
  • .
  • another problem with for..in is that it enumerates all enumerable properties, so when an object instance is created, enumerable properties inherited from the prototype will also be traversed, so if (foo.hasOwnProperty) has to be written in some cases, which is troublesome
  • .
  • using map, filter, reduce, etc., is the same in result as the equivalent for..in or for..of, but the idea of writing code is different. The former is logic-oriented, while the latter is process-oriented
  • .
  • Logic-oriented is a feature of functional programming. If you know a little about functional things, there will be a very important concept of pure function (what is specific can be looked up online). Generally speaking, business is complex, and writing large-scale applications is difficult largely because of high business complexity. If we can guarantee that the code that implements the business, it is possible to have the characteristics of pure function. Then it can effectively reduce the complexity of the project and reduce the risk

the above is the answer to the first point in your question description. Let's talk about the second point.

if you want to jump out early using map or forEach, you can do this:

try {
arr.forEach(function(e){
  // todo something...
  throw new Error('foo')  
})
} catch(e) {}

although the method is partial to hack, it is really useful.

but then again, using these methods pays attention to the abstraction of mapping relations. If the logic is reasonably abstracted, the time loss of doing several more loops is really negligible in terms of performance. On the contrary, if you use some functional toollibraries to combine these business codes with (compose), you can improve performance.

the subject owner has time to learn about ramdajs as a tool library.

Menu