From 23f0985a8593d7641e6379036daf76070a7eadf8 Mon Sep 17 00:00:00 2001 From: Chris Kobrzak Date: Tue, 2 Jun 2015 17:10:04 +0100 Subject: [PATCH] Add approach to "constants" in ES5 --- es5/README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/es5/README.md b/es5/README.md index aec47d7ebc..02b516a55f 100644 --- a/es5/README.md +++ b/es5/README.md @@ -14,6 +14,7 @@ 1. [Functions](#functions) 1. [Properties](#properties) 1. [Variables](#variables) + 1. [Constants](#constants) 1. [Hoisting](#hoisting) 1. [Comparison Operators & Equality](#comparison-operators--equality) 1. [Blocks](#blocks) @@ -367,7 +368,7 @@ var superPower = new SuperPower(); ``` - - Use single `var` declaration for multiple variables. + - Use a single `var` declaration for multiple variables. Having one `var` declaration per variable is an unnecessary code repetition and it should hardly ever be a problem anyway as you always want to strive at simple functions with as few local variables as possible. If you need @@ -479,6 +480,23 @@ **[⬆ back to top](#table-of-contents)** +## Constants + + - JavaScript doesn't support a notion of constants so we resort to normal + class variables in their absence. So to denote a variable is meant to be + treated like a constant you should use the ALL_CAPS notation, e.g.: + + ``` + WHICH.MyClass.MY_CONSTANT = 'fooBar'; + ``` + + Please note, it is still possible to assign a different value to this kind + of variables so they should only be used as read-only properties. If you + need to change their values, use a standard variables notation. + +**[⬆ back to top](#table-of-contents)** + + ## Hoisting - Variable declarations get hoisted to the top of their scope, but their assignment does not.