diff options
author | Phil Hughes <me@iamphill.com> | 2017-04-20 18:52:01 +0300 |
---|---|---|
committer | Phil Hughes <me@iamphill.com> | 2017-04-20 18:52:01 +0300 |
commit | 9e4908afa6597bf598c24fa54ce13d6efb57cf38 (patch) | |
tree | 3d49836cc7944a569dbbd52d7e157a26e932a929 | |
parent | 9bef6ce67eb15eeb9ad762682d95adc1cca4fe28 (diff) | |
parent | c7049ed07f411337f5f0569197f2b05649ec6038 (diff) |
Merge branch 'doc-js-side-effetcs' into 'master'
Adds documentation entry: Don't user forEach, aim for code without side effects
See merge request !10811
-rw-r--r-- | doc/development/fe_guide/style_guide_js.md | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/doc/development/fe_guide/style_guide_js.md b/doc/development/fe_guide/style_guide_js.md index ed656476a96..038a689c09a 100644 --- a/doc/development/fe_guide/style_guide_js.md +++ b/doc/development/fe_guide/style_guide_js.md @@ -168,6 +168,23 @@ See [our current .eslintrc][eslintrc] for specific rules and patterns. - Avoid constructors with side-effects +- Prefer `.map`, `.reduce` or `.filter` over `.forEach` +A forEach will cause side effects, it will be mutating the array being iterated. Prefer using `.map`, +`.reduce` or `.filter` + + ```javascript + const users = [ { name: 'Foo' }, { name: 'Bar' } ]; + + // bad + users.forEach((user, index) => { + user.id = index; + }); + + // good + const usersWithId = users.map((user, index) => { + return Object.assign({}, user, { id: index }); + }); + ``` #### Parse Strings into Numbers - `parseInt()` is preferable over `Number()` or `+` |