From 6d9a787ae2b238aadd9f44b093d3d5970fc9a1a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alican=20=C3=87ubuk=C3=A7uo=C4=9Flu?= Date: Tue, 29 Dec 2015 19:11:52 +0200 Subject: [PATCH] Make reassigning a separate rule. --- README.md | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ccffd213..b3d0630d 100644 --- a/README.md +++ b/README.md @@ -631,7 +631,23 @@ Other Style Guides - [7.12](#7.12) Never mutate parameters. - > Why? Overwriting parameters can lead to unexpected behavior, especially when accessing the `arguments` object. Manipulating objects passed in as parameters can cause unwanted variable side effects in the original caller. + > Why? Manipulating objects passed in as parameters can cause unwanted variable side effects in the original caller. + + ```javascript + // bad + function f1(obj) { + obj.key = 1; + }; + + // good + function f2(obj) { + const key = Object.prototype.hasOwnProperty.call(obj, 'key') ? obj.key : 1; + }; + ``` + +- [7.13](#7.13) Never reassign parameters. + + > Why? Reassigning parameters can lead to unexpected behavior, especially when accessing the `arguments` object. It can also cause optimization issues, especially in V8. eslint rules: [`no-param-reassign`](http://eslint.org/docs/rules/no-param-reassign.html). @@ -645,21 +661,13 @@ Other Style Guides if (!a) { a = 1; } } - function f3(obj) { - obj.key = 1; - }; - // good - function f4(a) { + function f3(a) { const b = a || 1; } - function f5(a = 1) { + function f4(a = 1) { } - - function f6(obj) { - const key = Object.prototype.hasOwnProperty.call(obj, 'key') ? obj.key : 1; - }; ``` **[⬆ back to top](#table-of-contents)**