In most programming languages, foreach不仅仅是语法糖,通过编译优化它可以提供更好的性能,比如直接略去边界检查。为了能更好地做到这些优化,foreachalso imposes design constraints. For example, in C#, the iterative process does not allow changes to the container itself (adding or deleting elements), and breaks are not allowed (we know that all conditional statements will reduce the performance of the instruction cache and pipeline).
Angular wraps a series of native JS methods to better monitor model changes. The usage of these JS methods is basically the same as before. The forEach就是一个,还有$timeout you mentioned is just one, and there are also $timeout and so on. For information about Angular’s data binding method, you can see this: http://harttle.com/2015/06/06/angular-data-binding-and-digest.html
Js’s native forEach and jquery’s each are like this, return true, interrupt the subsequent operation, and continue traversing to the next operation, similar to continue; return false, end the entire traversal, similar to break
I'm not sure about Angular. You can give it a try. This kind of syntactic sugar should be similar.
Let me answer your question first:
forEach
cannot break out of the loop.In most programming languages,
foreach
不仅仅是语法糖,通过编译优化它可以提供更好的性能,比如直接略去边界检查。为了能更好地做到这些优化,foreach
also imposes design constraints. For example, in C#, the iterative process does not allow changes to the container itself (adding or deleting elements), and breaks are not allowed (we know that all conditional statements will reduce the performance of the instruction cache and pipeline).Angular wraps a series of native JS methods to better monitor model changes. The usage of these JS methods is basically the same as before. The
forEach
就是一个,还有$timeout
you mentioned is just one, and there are also$timeout
and so on. For information about Angular’s data binding method, you can see this: http://harttle.com/2015/06/06/angular-data-binding-and-digest.htmlhttp://stackoverflow.com/a/13844508/2586541
Js’s native forEach and jquery’s each are like this, return true, interrupt the subsequent operation, and continue traversing to the next operation, similar to continue; return false, end the entire traversal, similar to break
I'm not sure about Angular. You can give it a try. This kind of syntactic sugar should be similar.
@harttle When the looped data is too large, it won’t affect the performance very much.