This document serves as the complete definition of Google’s coding standards for source code in the JavaScript programming language. A JavaScript source file is described as being in Google Style if and only if it adheres to the rules herein Show
Like other programming style guides, the issues covered span not only aesthetic issues of formatting, but other types of conventions or coding standards as well. However, this document focuses primarily on the hard-and-fast rules that we follow universally, and avoids giving advice that isn't clearly enforceable (whether by human or tool) 1. 1 Terminology notesIn this document, unless otherwise clarified
Other terminology notes will appear occasionally throughout the document 1. 2 Guide notesExample code in this document is non-normative. That is, while the examples are in Google Style, they may not illustrate the only stylish way to represent the code. Optional formatting choices made in examples must not be enforced as rules 2 Source file basics2. 1 File nameFile names must be all lowercase and may include underscores ( 49) or dashes ( 50), but no additional punctuation. Follow the convention that your project uses. Filenames’ extension must be 512. 2 File encoding. UTF-8Source files are encoded in UTF-8 2. 3 Special characters2. 3. 1 Whitespace charactersAside from the line terminator sequence, the ASCII horizontal space character (0x20) is the only whitespace character that appears anywhere in a source file. This implies that
2. 3. 2 Special escape sequencesFor any character that has a special escape sequence ( 52, 53, 54, 55, 56, 57, 58, 59, 60), that sequence is used rather than the corresponding numeric escape (e. g 61, 62, or 63). Legacy octal escapes are never used2. 3. 3 Non-ASCII charactersFor the remaining non-ASCII characters, either the actual Unicode character (e. g. 64) or the equivalent hex or Unicode escape (e. g. 65) is used, depending only on which makes the code easier to read and understandTip. In the Unicode escape case, and occasionally even when actual Unicode characters are used, an explanatory comment can be very helpful
Tip. Never make your code less readable simply out of fear that some programs might not handle non-ASCII characters properly. If that happens, those programs are broken and they must be fixed 3 Source file structureAll new source files should either be a 66 file (a file containing a 66 call) or an ECMAScript (ES) module (uses 68 and 69 statements). Files consist of the following, in order
Exactly one blank line separates each section that is present, except the file's implementation, which may be preceded by 1 or 2 blank lines 3. 1 License or copyright information, if presentIf license or copyright information belongs in a file, it belongs here 3. 2 /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 70 JSDoc, if presentSee for formatting rules 3. 3 /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 66 statementAll 66 files must declare exactly one 66 name on a single line. lines containing a 66 declaration must not be wrapped, and are therefore an exception to the 80-column limitThe entire argument to goog. module is what defines a namespace. It is the package name (an identifier that reflects the fragment of the directory structure where the code lives) plus, optionally, the main class/enum/interface that it defines concatenated to the end Example
3. 3. 1 HierarchyModule namespaces may never be named as a direct child of another module's namespace Disallowed
The directory hierarchy reflects the namespace hierarchy, so that deeper-nested children are subdirectories of higher-level parent directories. Note that this implies that owners of “parent” namespace groups are necessarily aware of all child namespaces, since they exist in the same directory 3. 3. 2 /* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
|