From 92a66ccc7caeaf935653b5e9fed6d02f90ce6805 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 12 Apr 2016 15:27:09 -0700 Subject: [PATCH] =?UTF-8?q?[guide]=20[breaking]=20Disallow=20leading=20und?= =?UTF-8?q?erscores=20-=20there=E2=80=99s=20no=20such=20thing=20as=20?= =?UTF-8?q?=E2=80=9Cprivate=20properties=E2=80=9D.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per https://github.com/airbnb/javascript/issues/490#issuecomment-209126331 --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c7254769..39ce51f1 100644 --- a/README.md +++ b/README.md @@ -2294,15 +2294,18 @@ Other Style Guides ``` - - [22.4](#naming--leading-underscore) Use a leading underscore `_` when naming private properties. eslint: [`no-underscore-dangle`](http://eslint.org/docs/rules/no-underscore-dangle.html) jscs: [`disallowDanglingUnderscores`](http://jscs.info/rule/disallowDanglingUnderscores) + - [22.4](#naming--leading-underscore) Do not use trailing or leading underscores. eslint: [`no-underscore-dangle`](http://eslint.org/docs/rules/no-underscore-dangle.html) jscs: [`disallowDanglingUnderscores`](http://jscs.info/rule/disallowDanglingUnderscores) + + > Why? JavaScript does not have the concept of privacy in terms of properties or methods. Although a leading underscore is a common convention to mean “private”, in fact, these properties are fully public, and as such, are part of your public API contract. This convention might lead developers to wrongly think that a change won't count as breaking, or that tests aren't needed. tl;dr: if you want something to be “private”, it must not be observably present. ```javascript // bad this.__firstName__ = 'Panda'; this.firstName_ = 'Panda'; + this._firstName = 'Panda'; // good - this._firstName = 'Panda'; + this.firstName = 'Panda'; ```