Cara menggunakan latihan logika javascript

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 notes

In this document, unless otherwise clarified

  1. The term comment always refers to implementation comments. We do not use the phrase documentation comments, instead using the common term “JSDoc” for both human-readable text and machine-readable annotations within

    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    48

  2. This Style Guide uses RFC 2119 terminology when using the phrases must, must not, should, should not, and may. The terms prefer and avoid correspond to should and should not, respectively. Imperative and declarative statements are prescriptive and correspond to must

Other terminology notes will appear occasionally throughout the document

1. 2 Guide notes

Example 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 basics

2. 1 File name

File names must be all lowercase and may include underscores (

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
49) or dashes (
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
50), but no additional punctuation. Follow the convention that your project uses. Filenames’ extension must be
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
51

2. 2 File encoding. UTF-8

Source files are encoded in UTF-8

2. 3 Special characters

2. 3. 1 Whitespace characters

Aside 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

  1. All other whitespace characters in string literals are escaped, and

  2. Tab characters are not used for indentation

2. 3. 2 Special escape sequences

For any character that has a special escape sequence (

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
52,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
53,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
54,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
55,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
56,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
57,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
58,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
59,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
60), that sequence is used rather than the corresponding numeric escape (e. g
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
61,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
62, or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
63). Legacy octal escapes are never used

2. 3. 3 Non-ASCII characters

For the remaining non-ASCII characters, either the actual Unicode character (e. g.

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
64) or the equivalent hex or Unicode escape (e. g.
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
65) is used, depending only on which makes the code easier to read and understand

Tip. In the Unicode escape case, and occasionally even when actual Unicode characters are used, an explanatory comment can be very helpful

/* Best: perfectly clear even without a comment. */
const units = 'μs';

/* Allowed: but unnecessary as μ is a printable character. */
const units = '\u03bcs'; // 'μs'

/* Good: use escapes for non-printable characters with a comment for clarity. */
return '\ufeff' + content;  // Prepend a byte order mark.
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';

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 structure

All new source files should either be a

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 file (a file containing a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 call) or an ECMAScript (ES) module (uses
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
68 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
69 statements). Files consist of the following, in order

  1. License or copyright information, if present
  2. /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    70 JSDoc, jika ada
  3. /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    66 statement, if a
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    66 file
  4. ES
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    68 statements, if an ES module
  5. /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    74 and
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    75 statements
  6. The file’s implementation

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

If 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 present

See for formatting rules

3. 3 /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 66 statement

All

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 files must declare exactly one
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 name on a single line. lines containing a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 declaration must not be wrapped, and are therefore an exception to the 80-column limit

The 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

goog.module('search.urlHistory.UrlHistoryService');

3. 3. 1 Hierarchy

Module namespaces may never be named as a direct child of another module's namespace

Disallowed

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');

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';
81

The single

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 statement may optionally be followed by a call to
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
83. Hindari
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_84 jika memungkinkan

Example

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_81 ada untuk memudahkan transisi dari ruang nama berbasis hierarki objek tradisional tetapi dilengkapi dengan beberapa batasan penamaan. As the child module name must be created after the parent namespace, this name must not be a child or parent of any other
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 (for example,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
87 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
88 cannot both exist safely, nor can
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
87 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
90)

3. 3. 3 /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 66 Exports

Classes, enums, functions, constants, and other symbols are exported using the

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
92 object. Exported symbols may be defined directly on the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
92 object, or else declared locally and exported separately. Symbols are only exported if they are meant to be used outside the module. Simbol modul-lokal yang tidak diekspor tidak dideklarasikan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 dan namanya juga tidak diakhiri dengan garis bawah. Tidak ada pemesanan yang ditentukan untuk simbol ekspor dan modul-lokal

Contoh

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';

Jangan membubuhi keterangan objek

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_92 sebagai
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 karena sudah diperlakukan sebagai konstanta oleh kompiler

/** @const */
exports = {exportedFunction};

3. 4 modul ES

3. 4. 1 Impor

Pernyataan impor tidak boleh dibungkus garis dan karenanya merupakan pengecualian untuk batas 80 kolom

3. 4. 1. 1 Jalur impor

File modul ES harus menggunakan pernyataan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_68 untuk mengimpor file modul ES lainnya. Jangan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_74 modul ES lainnya

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
3. 4. 1. 1. 1 Ekstensi file di jalur impor

Ekstensi file

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
51 tidak opsional di jalur impor dan harus selalu disertakan

import '../directory/file';
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
0

Jangan mengimpor file yang sama berkali-kali. Hal ini dapat mempersulit penentuan impor agregat suatu file

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_1
3. 4. 1. 3 Penamaan impor
3. 4. 1. 3. 1 Impor modul penamaan

Nama impor modul (

goog.module('search.urlHistory.UrlHistoryService');
00) adalah
goog.module('search.urlHistory.UrlHistoryService');
01 nama yang berasal dari nama file yang diimpor

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
2
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
33. 4. 1. 3. 2 Naming default imports

Default import names are derived from the imported file name and follow the rules in

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
4

Note. In general this should not happen as default exports are banned by this style guide, see . Default imports are only used to import modules that do not conform to this style guide

3. 4. 1. 3. 3 Naming named imports

In general symbols imported via the named import (

goog.module('search.urlHistory.UrlHistoryService');
02) should keep the same name. Avoid aliasing imports (
goog.module('search.urlHistory.UrlHistoryService');
03). Prefer fixing name collisions by using a module import (
goog.module('search.urlHistory.UrlHistoryService');
04) or renaming the exports themselves

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
5

If renaming a named import is needed then use components of the imported module's file name or path in the resulting alias

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
6

3. 4. 2 Ekspor

Simbol hanya diekspor jika dimaksudkan untuk digunakan di luar modul. Simbol modul-lokal yang tidak diekspor tidak dideklarasikan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 dan namanya juga tidak diakhiri dengan garis bawah. Tidak ada pemesanan yang ditentukan untuk simbol ekspor dan modul-lokal

3. 4. 2. 1 Ekspor bernama vs default

Gunakan ekspor bernama di semua kode. Anda dapat menerapkan kata kunci

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_69 ke deklarasi, atau menggunakan sintaks
goog.module('search.urlHistory.UrlHistoryService');
07

Jangan gunakan ekspor default. Mengimpor modul harus memberi nama pada nilai-nilai ini, yang dapat menyebabkan ketidakkonsistenan dalam penamaan di seluruh modul

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
7
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
8
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
9
3. 4. 2. 2 Mengekspor kelas dan objek kontainer statis

Jangan mengekspor kelas atau objek kontainer dengan metode atau properti statis demi namespace

goog.module('search.urlHistory.UrlHistoryService');
_0

Alih-alih, ekspor konstanta dan fungsi individual

goog.module('search.urlHistory.UrlHistoryService');
_1
3. 4. 2. 3 Mutabilitas ekspor

Variabel yang diekspor tidak boleh dimutasi di luar inisialisasi modul

Ada alternatif jika diperlukan mutasi, termasuk mengekspor referensi konstanta ke objek yang memiliki bidang yang dapat diubah atau mengekspor fungsi pengakses untuk data yang dapat diubah

________35
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_35_______3
3. 4. 2. 4 ekspor dari

goog.module('search.urlHistory.UrlHistoryService');
_08 pernyataan tidak boleh dibungkus garis dan karenanya merupakan pengecualian untuk batas 80 kolom. Ini berlaku untuk kedua rasa
goog.module('search.urlHistory.UrlHistoryService');
08

goog.module('search.urlHistory.UrlHistoryService');
_4

3. 4. 3 Dependensi Edaran dalam modul ES

Jangan membuat siklus antar modul ES, meskipun spesifikasi ECMAScript mengizinkannya. Perhatikan bahwa dimungkinkan untuk membuat siklus dengan pernyataan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
68 dan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
69

________35
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_35_______6
goog.module('search.urlHistory.UrlHistoryService');
7

3. 4. 4 Interoperasi dengan Penutupan

3. 4. 4. 1 referensi Google

Untuk mereferensikan namespace

goog.module('search.urlHistory.UrlHistoryService');
12 Penutupan, impor
goog.module('search.urlHistory.UrlHistoryService');
13 Penutupan

goog.module('search.urlHistory.UrlHistoryService');
_8

goog.module('search.urlHistory.UrlHistoryService');
_13 hanya mengekspor sebagian properti dari
goog.module('search.urlHistory.UrlHistoryService');
12 global yang dapat digunakan dalam modul ES

3. 4. 4. 2 google. diperlukan dalam modul ES

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 dalam modul ES berfungsi seperti halnya dalam file
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66. Anda dapat meminta simbol ruang nama Penutupan (mis. e. , simbol yang dibuat oleh
goog.module('search.urlHistory.UrlHistoryService');
18 atau
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66) dan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 akan mengembalikan nilai

goog.module('search.urlHistory.UrlHistoryService');
_9
3. 4. 4. 3 Mendeklarasikan ID Modul Penutupan dalam modul ES

goog.module('search.urlHistory.UrlHistoryService');
_21 dapat digunakan dalam modul ES untuk mendeklarasikan ID modul seperti
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66. Artinya, ID modul ini bisa
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_74d,
goog.module('search.urlHistory.UrlHistoryService');
24d,
goog.module('search.urlHistory.UrlHistoryService');
25'd, dll. seolah-olah itu adalah
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_66 yang tidak memanggil
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
81. Itu tidak membuat ID modul sebagai simbol JavaScript yang tersedia secara global

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 (atau
goog.module('search.urlHistory.UrlHistoryService');
24) untuk ID modul dari
goog.module('search.urlHistory.UrlHistoryService');
21 akan selalu mengembalikan objek modul (seolah-olah itu adalah
goog.module('search.urlHistory.UrlHistoryService');
04'd). Akibatnya, argumen untuk
goog.module('search.urlHistory.UrlHistoryService');
_21 harus selalu diakhiri dengan
goog.module('search.urlHistory.UrlHistoryService');
33

Catatan. Merupakan kesalahan untuk memanggil

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_81 dalam modul ES, itu hanya dapat dipanggil dari file
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66. Tidak ada cara langsung untuk mengaitkan namespace lawas dengan modul ES

goog.module('search.urlHistory.UrlHistoryService');
_21 hanya boleh digunakan untuk memutakhirkan file Penutupan ke modul ES di tempat, di mana ekspor bernama digunakan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
0

3. 5 goog.module('search.urlHistory.UrlHistoryService'); _37

Dalam file

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_66, pernyataan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 secara opsional dapat diikuti dengan panggilan ke
goog.module('search.urlHistory.UrlHistoryService');
40

Dalam modul ES, pernyataan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
68 secara opsional dapat diikuti dengan panggilan ke
goog.module('search.urlHistory.UrlHistoryService');
40

3. 6 /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 74 dan /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 75 pernyataan

Impor dilakukan dengan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 dan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 pernyataan. Nama-nama yang diimpor oleh pernyataan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 dapat digunakan baik dalam kode maupun dalam anotasi tipe, sementara yang diimpor oleh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 hanya dapat digunakan dalam anotasi tipe

Pernyataan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 dan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 membentuk blok yang berdekatan tanpa baris kosong. Blok ini mengikuti deklarasi
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_66 dipisahkan. Seluruh argumen untuk
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 atau
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 adalah ruang nama yang ditentukan oleh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 dalam file terpisah.
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 dan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 pernyataan mungkin tidak muncul di tempat lain dalam file

Each

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 is assigned to a single constant alias, or else destructured into several constant aliases. Alias ​​​​ini adalah satu-satunya cara yang dapat diterima untuk merujuk ke dependensi dalam anotasi jenis atau kode. Ruang nama yang sepenuhnya memenuhi syarat tidak boleh digunakan di mana pun, kecuali sebagai argumen untuk
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 atau
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75

Pengecualian. Jenis, variabel, dan fungsi yang dideklarasikan dalam file externs harus menggunakan nama yang memenuhi syarat dalam jenis anotasi dan kode

Alias ​​​​harus cocok dengan komponen akhir yang dipisahkan titik dari namespace modul yang diimpor

Pengecualian. Dalam kasus tertentu, komponen tambahan namespace dapat digunakan untuk membentuk alias yang lebih panjang. Alias ​​​​yang dihasilkan harus mempertahankan casing pengidentifikasi asli sedemikian rupa sehingga masih mengidentifikasi jenisnya dengan benar. Alias ​​​​yang lebih panjang dapat digunakan untuk memperjelas alias yang identik, atau jika itu meningkatkan keterbacaan secara signifikan. Selain itu, alias yang lebih panjang harus digunakan untuk mencegah penyamaran jenis asli seperti

goog.module('search.urlHistory.UrlHistoryService');
61,
goog.module('search.urlHistory.UrlHistoryService');
62,
goog.module('search.urlHistory.UrlHistoryService');
63,
goog.module('search.urlHistory.UrlHistoryService');
64, dan
goog.module('search.urlHistory.UrlHistoryService');
65 (untuk daftar yang lebih lengkap, lihat Objek Bawaan Standar dan API Web di MDN). Saat mengganti nama alias yang dirusak, spasi harus mengikuti titik dua seperti yang dipersyaratkan di

File tidak boleh berisi pernyataan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_74 dan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 untuk namespace yang sama. Jika nama yang diimpor digunakan baik dalam kode maupun anotasi tipe, nama tersebut harus diimpor dengan satu pernyataan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74

Jika modul diimpor hanya untuk efek sampingnya, panggilan harus berupa

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 (bukan
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75) dan penetapan dapat dihilangkan. Komentar diperlukan untuk menjelaskan mengapa ini diperlukan dan menekan peringatan kompiler

Garis diurutkan menurut aturan berikut. Semua persyaratan dengan nama di sebelah kiri didahulukan, diurutkan menurut abjad berdasarkan nama tersebut. Kemudian diperlukan destrukturisasi, sekali lagi diurutkan berdasarkan nama di sisi kiri. Terakhir, panggilan apa pun yang diperlukan yang berdiri sendiri (umumnya ini untuk modul yang diimpor hanya untuk efek sampingnya)

Tip. Tidak perlu menghafal perintah ini dan menegakkannya secara manual. Anda dapat mengandalkan IDE Anda untuk melaporkan persyaratan yang tidak disortir dengan benar

Jika nama alias atau modul yang panjang akan menyebabkan garis melebihi batas 80 kolom, baris tersebut tidak boleh dibungkus. baris yang diperlukan merupakan pengecualian untuk batas 80 kolom

Example

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
1

Patah semangat

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
2

Disallowed

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
3

3. 7 Implementasi file

Implementasi sebenarnya mengikuti setelah semua informasi dependensi dideklarasikan (dipisahkan oleh setidaknya satu baris kosong)

Ini dapat terdiri dari deklarasi modul-lokal (konstanta, variabel, kelas, fungsi, dll), serta simbol yang diekspor

4 Pemformatan

Catatan Terminologi. konstruksi seperti blok mengacu pada tubuh kelas, fungsi, metode, atau blok kode yang dipisahkan oleh kurung kurawal. Perhatikan bahwa, by dan , setiap literal array atau objek dapat secara opsional diperlakukan seolah-olah itu adalah konstruksi seperti blok

Tip. Gunakan

goog.module('search.urlHistory.UrlHistoryService');
_71. Komunitas JavaScript telah menginvestasikan upaya untuk memastikan dentang-format melakukan hal yang benar pada file JavaScript.
goog.module('search.urlHistory.UrlHistoryService');
_71 memiliki integrasi dengan beberapa editor populer

4. 1 kawat gigi

4. 1. 1 Kawat gigi digunakan untuk semua struktur kontrol

Kawat gigi diperlukan untuk semua struktur kontrol (mis. e.

goog.module('search.urlHistory.UrlHistoryService');
_73,
goog.module('search.urlHistory.UrlHistoryService');
74,
goog.module('search.urlHistory.UrlHistoryService');
75,
goog.module('search.urlHistory.UrlHistoryService');
76,
goog.module('search.urlHistory.UrlHistoryService');
77, serta yang lainnya), bahkan jika tubuh hanya berisi satu pernyataan. Pernyataan pertama dari blok yang tidak kosong harus dimulai pada barisnya sendiri

Disallowed

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
4

Pengecualian. Sebuah pernyataan if sederhana yang dapat dimuat seluruhnya pada satu baris tanpa pembungkus (dan yang tidak memiliki else) dapat disimpan pada satu baris tanpa tanda kurung saat itu meningkatkan keterbacaan. Ini adalah satu-satunya kasus di mana struktur kontrol dapat menghilangkan tanda kurung dan baris baru

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
5

4. 1. 2 blok kosong. gaya K&R

Kawat gigi mengikuti gaya Kernighan dan Ritchie (tanda kurung Mesir) untuk balok kosong dan konstruksi seperti balok

  • Tidak ada jeda baris sebelum kurung kurawal pembuka
  • Jeda baris setelah kurung kurawal pembuka
  • Jeda baris sebelum kurung kurawal penutup
  • Jeda baris setelah kurung kurawal penutup jika kurung kurawal itu mengakhiri pernyataan atau badan fungsi atau pernyataan kelas, atau metode kelas. Khususnya, tidak ada jeda baris setelah kurung kurawal jika diikuti oleh ________ 35 _______74, ________35 ______79, ________35 _______77, atau tanda koma, titik koma, atau tanda kurung kanan

Example

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
6

4. 1. 3 Blok kosong. Mungkin ringkas

Blok kosong atau konstruksi seperti blok dapat ditutup segera setelah dibuka, tanpa karakter, spasi, atau jeda baris di antaranya (i. e.

goog.module('search.urlHistory.UrlHistoryService');
_81), kecuali itu adalah bagian dari pernyataan multi-blok (yang secara langsung berisi banyak blok).
goog.module('search.urlHistory.UrlHistoryService');
73/
goog.module('search.urlHistory.UrlHistoryService');
74 atau
goog.module('search.urlHistory.UrlHistoryService');
84/
goog.module('search.urlHistory.UrlHistoryService');
79/
goog.module('search.urlHistory.UrlHistoryService');
86)

Example

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
7

Disallowed

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
8

4. 2 Blokir lekukan. +2 spasi

Setiap kali blok baru atau konstruksi seperti blok dibuka, indentasi bertambah dua spasi. Saat blok berakhir, indentasi kembali ke level indentasi sebelumnya. Level indent berlaku untuk kode dan komentar di seluruh blok. (Lihat contoh di )

4. 2. 1 Array literal. opsional seperti blok

Array literal apa pun secara opsional dapat diformat seolah-olah itu adalah "konstruk seperti blok". ” Misalnya, berikut ini semuanya valid (bukan daftar lengkap)

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
9
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
0

Kombinasi lain diperbolehkan, terutama ketika menekankan pengelompokan semantik antar elemen, tetapi tidak boleh digunakan hanya untuk mengurangi ukuran vertikal array yang lebih besar

4. 2. 2 Objek literal. opsional seperti blok

Objek literal apa pun secara opsional dapat diformat seolah-olah itu adalah "konstruk seperti blok". ” Contoh yang sama berlaku sebagai. Misalnya, berikut ini semuanya valid (bukan daftar lengkap)

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
1
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
2

4. 2. 3 Kelas literal

Literal kelas (baik deklarasi atau ekspresi) diindentasi sebagai blok. Jangan tambahkan titik koma setelah metode, atau setelah kurung kurawal penutup deklarasi kelas (pernyataan—seperti tugas—yang berisi ekspresi kelas masih diakhiri dengan titik koma). Gunakan kata kunci

goog.module('search.urlHistory.UrlHistoryService');
87, tetapi bukan anotasi JSDoc ________35______88 kecuali jika kelas memperluas tipe template

Example

________41
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_41_______4

4. 2. 4 Ekspresi fungsi

Saat mendeklarasikan fungsi anonim dalam daftar argumen untuk pemanggilan fungsi, isi fungsi diindentasi dua spasi lebih banyak dari kedalaman indentasi sebelumnya

Example

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_5

4. 2. 5 Tukar pernyataan

Seperti halnya blok lainnya, isi blok sakelar diberi indentasi +2

After a switch label, a newline appears, and the indentation level is increased +2, exactly as if a block were being opened. An explicit block may be used if required by lexical scoping. The following switch label returns to the previous indentation level, as if a block had been closed

A blank line is optional between a

goog.module('search.urlHistory.UrlHistoryService');
89 and the following case

Example

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
6

4. 3 Statements

4. 3. 1 One statement per line

Each statement is followed by a line-break

4. 3. 2 Semicolons are required

Every statement must be terminated with a semicolon. Relying on automatic semicolon insertion is forbidden

4. 4 Column limit. 80

JavaScript code has a column limit of 80 characters. Except as noted below, any line that would exceed this limit must be line-wrapped, as explained in

Exceptions

  1. /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    66,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    74 and
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    75 statements (see and )
  2. ES module
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    68 and
    goog.module('search.urlHistory.UrlHistoryService');
    
    08 statements (see and )
  3. Lines where obeying the column limit is not possible or would hinder discoverability. Examples include
    • A long URL which should be clickable in source
    • A shell command intended to be copied-and-pasted
    • A long string literal which may need to be copied or searched for wholly (e. g. , a long file path)

4. 5 Line-wrapping

Terminology Note. Line wrapping is breaking a chunk of code into multiple lines to obey column limit, where the chunk could otherwise legally fit in a single line

There is no comprehensive, deterministic formula showing exactly how to line-wrap in every situation. Very often there are several valid ways to line-wrap the same piece of code

Note. While the typical reason for line-wrapping is to avoid overflowing the column limit, even code that would in fact fit within the column limit may be line-wrapped at the author's discretion

Tip. Extracting a method or local variable may solve the problem without the need to line-wrap

4. 5. 1 Where to break

The prime directive of line-wrapping is. prefer to break at a higher syntactic level

Preferred

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
7

Patah semangat

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
8

In the preceding example, the syntactic levels from highest to lowest are as follows. assignment, division, function call, parameters, number constant

Operators are wrapped as follows

  1. When a line is broken at an operator the break comes after the symbol. (Note that this is not the same practice used in Google style for Java. )
    1. Ini tidak berlaku untuk titik (
      goog.module('search.urlHistory.UrlHistoryService');
      
      95), yang sebenarnya bukan operator
  2. A method or constructor name stays attached to the open parenthesis (
    goog.module('search.urlHistory.UrlHistoryService');
    
    96) that follows it
  3. A comma (
    goog.module('search.urlHistory.UrlHistoryService');
    
    97) stays attached to the token that precedes it

Note. The primary goal for line wrapping is to have clear code, not necessarily code that fits in the smallest number of lines

4. 5. 2 Indent continuation lines at least +4 spaces

When line-wrapping, each line after the first (each continuation line) is indented at least +4 from the original line, unless it falls under the rules of block indentation

When there are multiple continuation lines, indentation may be varied beyond +4 as appropriate. In general, continuation lines at a deeper syntactic level are indented by larger multiples of 4, and two lines use the same indentation level if and only if they begin with syntactically parallel elements

addresses the discouraged practice of using a variable number of spaces to align certain tokens with previous lines

4. 6 Whitespace

4. 6. 1 Vertical whitespace

A single blank line appears

  1. Between consecutive methods in a class or object literal
    1. Exception. A blank line between two consecutive properties definitions in an object literal (with no other code between them) is optional. Such blank lines are used as needed to create logical groupings of fields
  2. Within method bodies, sparingly to create logical groupings of statements. Blank lines at the start or end of a function body are not allowed
  3. Optionally before the first or after the last method in a class or object literal (neither encouraged nor discouraged)
  4. As required by other sections of this document (e. g. )

Multiple consecutive blank lines are permitted, but never required (nor encouraged)

4. 6. 2 Horizontal whitespace

Use of horizontal whitespace depends on location, and falls into three broad categories. leading (at the start of a line), trailing (at the end of a line), and internal. Leading whitespace (i. e. , indentation) is addressed elsewhere. Trailing whitespace is forbidden

Beyond where required by the language or other style rules, and apart from literals, comments, and JSDoc, a single internal ASCII space also appears in the following places only

  1. Separating any reserved word (such as
    goog.module('search.urlHistory.UrlHistoryService');
    
    73,
    goog.module('search.urlHistory.UrlHistoryService');
    
    75, or
    goog.module('search.urlHistory.UrlHistoryService');
    
    79) except for
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    01 and
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    02, from an open parenthesis (
    goog.module('search.urlHistory.UrlHistoryService');
    
    96) that follows it on that line
  2. Separating any reserved word (such as
    goog.module('search.urlHistory.UrlHistoryService');
    
    74 or
    goog.module('search.urlHistory.UrlHistoryService');
    
    79) from a closing curly brace (
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    06) that precedes it on that line
  3. Before any open curly brace (
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    07), with two exceptions
    1. Before an object literal that is the first argument of a function or the first element in an array literal (e. g.
      goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
      goog.module('foo.bar.baz');
      
      08)
    2. In a template expansion, as it is forbidden by the language (e. g. valid.
      goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
      goog.module('foo.bar.baz');
      
      09, invalid.
      goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
      goog.module('foo.bar.baz');
      
      10)
  4. On both sides of any binary or ternary operator
  5. After a comma (
    goog.module('search.urlHistory.UrlHistoryService');
    
    97) or semicolon (
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    12). Note that spaces are never allowed before these characters
  6. After the colon (
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    13) in an object literal
  7. On both sides of the double slash (
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    14) that begins an end-of-line comment. Here, multiple spaces are allowed, but not required
  8. After an open-block comment character and on both sides of close characters (e. g. for short-form type declarations, casts, and parameter name comments.
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    15; or
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    16; or
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    17)

4. 6. 3 Horizontal alignment. discouraged

Terminology Note. Horizontal alignment is the practice of adding a variable number of additional spaces in your code with the goal of making certain tokens appear directly below certain other tokens on previous lines

This practice is permitted, but it is generally discouraged by Google Style. Bahkan tidak diperlukan untuk mempertahankan perataan horizontal di tempat yang sudah digunakan

Berikut adalah contoh tanpa perataan, diikuti oleh satu dengan perataan. Keduanya diperbolehkan, tetapi yang terakhir tidak disarankan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_9

Tip. Penyelarasan dapat membantu keterbacaan, tetapi menimbulkan masalah untuk pemeliharaan di masa mendatang. Pertimbangkan perubahan di masa mendatang yang hanya perlu menyentuh satu baris. Perubahan ini dapat membuat pemformatan yang sebelumnya menyenangkan menjadi berantakan, dan itu diperbolehkan. Lebih sering itu meminta pembuat kode (mungkin Anda) untuk menyesuaikan spasi putih pada baris terdekat juga, mungkin memicu serangkaian pemformatan ulang yang mengalir. Perubahan satu baris itu sekarang memiliki radius ledakan. Hal ini paling buruk dapat mengakibatkan kesibukan yang sia-sia, tetapi paling-paling itu masih merusak informasi riwayat versi, memperlambat peninjau, dan memperburuk konflik penggabungan.

4. 6. 4 Argumen fungsi

Lebih suka meletakkan semua argumen fungsi pada baris yang sama dengan nama fungsi. Jika melakukannya akan melebihi batas 80 kolom, argumen harus dibungkus baris dengan cara yang dapat dibaca. Untuk menghemat ruang, Anda dapat membungkus sedekat mungkin dengan 80, atau meletakkan setiap argumen pada barisnya sendiri untuk meningkatkan keterbacaan. Lekukan harus empat spasi. Menyelaraskan dengan tanda kurung diperbolehkan, tetapi tidak disarankan. Di bawah ini adalah pola paling umum untuk pembungkusan argumen

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_0

4. 7 Tanda kurung pengelompokan. direkomendasikan

Tanda kurung pengelompokan opsional dihilangkan hanya jika penulis dan peninjau setuju bahwa tidak ada kemungkinan yang masuk akal bahwa kode akan disalahartikan tanpanya, juga tidak akan membuat kode lebih mudah dibaca. Tidak masuk akal untuk berasumsi bahwa setiap pembaca memiliki seluruh tabel prioritas operator yang dihafal

Jangan gunakan tanda kurung yang tidak perlu di sekitar seluruh ekspresi setelah

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
18,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
19,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
20,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
21,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
22,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
23,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_4,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
25, atau ________25, atau _______25

Tanda kurung diperlukan untuk gips tipe.

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_27

Bagian ini membahas komentar implementasi. JSDoc dialamatkan secara terpisah di

Komentar blok diindentasi pada tingkat yang sama dengan kode di sekitarnya. Mereka mungkin dalam gaya

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_28 atau
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
14. Untuk komentar multi-baris
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_28, baris berikutnya harus dimulai dengan * sejajar dengan
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
31 pada baris sebelumnya, untuk memperjelas komentar tanpa konteks tambahan

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_1

Komentar tidak diapit oleh kotak yang digambar dengan tanda bintang atau karakter lain

Jangan gunakan JSDoc (

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_48) untuk komentar implementasi

Komentar "Nama parameter" harus digunakan setiap kali nilai dan nama metode tidak cukup menyampaikan artinya, dan pemfaktoran ulang metode menjadi lebih jelas tidak mungkin dilakukan. Format pilihan mereka adalah sebelum nilai dengan =

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_2

Untuk konsistensi dengan kode di sekitarnya, Anda dapat menempatkannya setelah nilai tanpa =

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_3

5 Fitur bahasa

JavaScript menyertakan banyak fitur yang meragukan (dan bahkan berbahaya). Bagian ini menggambarkan fitur mana yang boleh atau tidak boleh digunakan, dan batasan tambahan apa pun dalam penggunaannya

5. 1 Deklarasi variabel lokal

5. 1. 1 Gunakan
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_33 dan
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
34

Deklarasikan semua variabel lokal dengan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33 atau
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
34. Gunakan const secara default, kecuali jika variabel perlu dipindahkan. Kata kunci
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_37 tidak boleh digunakan

5. 1. 2 Satu variabel per deklarasi

Setiap deklarasi variabel lokal hanya mendeklarasikan satu variabel. deklarasi seperti

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_38 tidak digunakan

5. 1. 3 Dideklarasikan saat dibutuhkan, diinisialisasi sesegera mungkin

Variabel lokal biasanya tidak dideklarasikan pada awal blok yang berisi atau konstruksi seperti blok. Sebaliknya, variabel lokal dideklarasikan dekat dengan titik mereka pertama kali digunakan (sesuai alasan), untuk meminimalkan cakupannya

5. 1. 4 Deklarasikan tipe sesuai kebutuhan

Anotasi jenis JSDoc dapat ditambahkan baik pada baris di atas deklarasi, atau sebaris sebelum nama variabel jika tidak ada JSDoc lain yang ada

Example

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_4

Mencampur gaya inline dan JSDoc tidak diperbolehkan. kompiler hanya akan memproses JsDoc pertama dan anotasi sebaris akan hilang

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
5

Tip. Ada banyak kasus di mana kompiler dapat menyimpulkan tipe template tapi bukan parameternya. Ini terutama terjadi ketika panggilan literal atau konstruktor inisialisasi tidak menyertakan nilai apa pun dari jenis parameter templat (e. g. , array kosong, objek,

goog.module('search.urlHistory.UrlHistoryService');
_64s, atau
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
40s), atau jika variabel dimodifikasi dalam penutupan. Anotasi jenis variabel lokal sangat membantu dalam kasus ini karena jika tidak, kompiler akan menganggap parameter templat sebagai tidak diketahui

5. 2 Literal array

5. 2. 1 Gunakan tanda koma

Sertakan tanda koma setiap kali ada jeda baris antara elemen terakhir dan braket penutup

Example

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_6

5. 2. 2 Jangan gunakan konstruktor variadic ________36______41

Konstruktor rawan kesalahan jika argumen ditambahkan atau dihapus. Gunakan literal sebagai gantinya

Disallowed

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_7

Ini berfungsi seperti yang diharapkan kecuali untuk kasus ketiga. jika

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_42 adalah bilangan bulat maka
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
43 adalah array dengan ukuran
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
42 di mana semua elemen adalah
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
45. Jika
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_42 adalah nomor lain, maka pengecualian akan dilemparkan, dan jika itu adalah nomor lain maka itu akan menjadi array elemen tunggal

Sebagai gantinya, tulislah

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_8

Secara eksplisit mengalokasikan array dengan panjang tertentu menggunakan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
47 diperbolehkan jika sesuai

5. 2. 3 Properti non-numerik

Jangan mendefinisikan atau menggunakan properti non-numerik pada array (selain

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
48). Gunakan
goog.module('search.urlHistory.UrlHistoryService');
_64 (atau
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
50) sebagai gantinya

5. 2. 4 Destrukturisasi

Literal array dapat digunakan di sisi kiri tugas untuk melakukan destrukturisasi (seperti saat membongkar beberapa nilai dari satu array atau iterable). Elemen sisa akhir dapat disertakan (tanpa spasi antara

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51 dan nama variabel). Elemen harus dihilangkan jika tidak digunakan

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_9

Destrukturisasi juga dapat digunakan untuk parameter fungsi (perhatikan bahwa nama parameter diperlukan tetapi diabaikan). Selalu tentukan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_52 sebagai nilai default jika parameter array yang dirusak adalah opsional, dan berikan nilai default di sisi kiri

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
0

Disallowed

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
_1

Tip. For (un)packing multiple values into a function’s parameter or return, prefer object destructuring to array destructuring when possible, as it allows naming the individual elements and specifying a different type for each

5. 2. 5 Spread operator

Array literals may include the spread operator (

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51) to flatten elements out of one or more other iterables. The spread operator should be used instead of more awkward constructs with
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
54. There is no space after the
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51

Example

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
2

5. 3 Object literals

5. 3. 1 Use trailing commas

Include a trailing comma whenever there is a line break between the final property and the closing brace

5. 3. 2 Do not use the
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
50 constructor

While

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
50 does not have the same problems as
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
41, it is still disallowed for consistency. Use an object literal (
goog.module('search.urlHistory.UrlHistoryService');
81 or
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
60) instead

5. 3. 3 Do not mix quoted and unquoted keys

Object literals may represent either structs (with unquoted keys and/or symbols) or dicts (with quoted and/or computed keys). Do not mix these key types in a single object literal

Disallowed

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
3

This also extends to passing the property name to functions, like

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
61. In particular, doing so will break in compiled code because the compiler cannot rename/obfuscate the string literal

Disallowed

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
4

This is best implemented as

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
5

5. 3. 4 Computed property names

Computed property names (e. g. ,

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
62) are allowed, and are considered dict-style (quoted) keys (i. e. , must not be mixed with non-quoted keys) unless the computed property is a symbol (e. g. ,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
63). Enum values may also be used for computed keys, but should not be mixed with non-enum keys in the same literal

5. 3. 5 Method shorthand

Methods can be defined on object literals using the method shorthand (

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
64) in place of a colon immediately followed by a
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
01 or arrow function literal

Example

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
6

Note that

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 in a method shorthand or
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
01 refers to the object literal itself whereas
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 in an arrow function refers to the scope outside the object literal

Example

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
7

5. 3. 6 Shorthand properties

Shorthand properties are allowed on object literals

Example

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
8

5. 3. 7 Destructuring

Object destructuring patterns may be used on the left-hand side of an assignment to perform destructuring and unpack multiple values from a single object

Destructured objects may also be used as function parameters, but should be kept as simple as possible. a single level of unquoted shorthand properties. Deeper levels of nesting and computed properties may not be used in parameter destructuring. Specify any default values in the left-hand-side of the destructured parameter (

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
69, rather than
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
70), and if a destructured object is itself optional, it must default to
goog.module('search.urlHistory.UrlHistoryService');
81. The JSDoc for the destructured parameter may be given any name (the name is unused but is required by the compiler)

Example

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
9

Disallowed

/** @const */
exports = {exportedFunction};
0

Destructuring may also be used for

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 statements, and in this case must not be wrapped. the entire statement occupies one line, regardless of how long it is (see )

5. 3. 8 Enums

Enumerations are defined by adding the

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
73 annotation to an object literal. Additional properties may not be added to an enum after it is defined. Enums must be constant, and all enum values must be deeply immutable

/** @const */
exports = {exportedFunction};
1

5. 4 Classes

5. 4. 1 Constructors

Constructors are optional. Subclass constructors must call

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
74 before setting any fields or otherwise accessing
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66. Interfaces should declare non-method properties in the constructor

5. 4. 2 Fields

Set all of a concrete object’s fields (i. e. all properties other than methods) in the constructor. Annotate fields that are never reassigned with

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 (these need not be deeply immutable). Annotate non-public fields with the proper visibility annotation (
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
78,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
79), and end all
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 fields' names with an underscore. Fields are never set on a concrete class'
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
81

Example

/** @const */
exports = {exportedFunction};
2

Tip. Properties should never be added to or removed from an instance after the constructor is finished, since it significantly hinders VMs’ ability to optimize. If necessary, fields that are initialized later should be explicitly set to

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
45 in the constructor to prevent later shape changes. Adding
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
83 to an object will check that undeclared properties are not added/accessed. Classes have this added by default

5. 4. 3 Computed properties

Computed properties may only be used in classes when the property is a symbol. Dict-style properties (that is, quoted or computed non-symbol keys, as defined in ) are not allowed. A

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
63 method should be defined for any classes that are logically iterable. Beyond this,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
85 should be used sparingly

Tip. hati-hati menggunakan simbol bawaan lainnya (mis. g. ,

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
86) as they are not polyfilled by the compiler and will therefore not work in older browsers

5. 4. 4 Static methods

Where it does not interfere with readability, prefer module-local functions over private static methods

Static methods should only be called on the base class itself. Static methods should not be called on variables containing a dynamic instance that may be either the constructor or a subclass constructor (and must be defined with

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
87 if this is done), and must not be called directly on a subclass that doesn’t define the method itself

Disallowed

/** @const */
exports = {exportedFunction};
3

5. 4. 5 Old-style class declarations

While ES6 classes are preferred, there are cases where ES6 classes may not be feasible. For example

  1. If there exist or will exist subclasses, including frameworks that create subclasses, that cannot be immediately changed to use ES6 class syntax. If such a class were to use ES6 syntax, all downstream subclasses not using ES6 class syntax would need to be modified

  2. Frameworks that require a known

    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    66 value before calling the superclass constructor, since constructors with ES6 super classes do not have access to the instance
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    66 value until the call to
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    02 returns

In all other ways the style guide still applies to this code.

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
34,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33, parameter default, istirahat, dan fungsi panah semuanya harus digunakan bila perlu

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
93 memungkinkan definisi mirip kelas yang mirip dengan sintaks kelas ES6

/** @const */
exports = {exportedFunction};
4

Alternatively, while

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
93 should be preferred for all new code, more traditional syntax is also allowed

/** @const */
exports = {exportedFunction};
5

Properti per-instance harus didefinisikan dalam konstruktor setelah panggilan ke konstruktor kelas super, jika ada kelas super. Methods should be defined on the prototype of the constructor

Defining constructor prototype hierarchies correctly is harder than it first appears. Oleh karena itu, sebaiknya gunakan ________36______95 dari Closure Library

5. 4. 6 Do not manipulate
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
81s directly

The

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
97 keyword allows clearer and more readable class definitions than defining
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
81 properties. Ordinary implementation code has no business manipulating these objects, though they are still useful for defining classes as defined in . Mixin dan memodifikasi prototipe objek bawaan dilarang secara eksplisit

Pengecualian. Kode framework (seperti Polymer, atau Angular) mungkin perlu menggunakan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
81s, dan sebaiknya tidak menggunakan solusi yang lebih buruk untuk menghindarinya

5. 4. 7 Getter dan Setter

Jangan gunakan properti pengambil dan penyetel JavaScript. Mereka berpotensi mengejutkan dan sulit untuk dipikirkan, dan memiliki dukungan terbatas dalam kompiler. Berikan metode biasa sebagai gantinya

Pengecualian. ada situasi di mana mendefinisikan pengambil atau penyetel tidak dapat dihindari (mis. g. kerangka pengikat data seperti Angular dan Polymer, atau untuk kompatibilitas dengan API eksternal yang tidak dapat disesuaikan). Dalam kasus ini saja, getter dan setter dapat digunakan dengan hati-hati, asalkan mereka didefinisikan dengan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
00 dan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
01 kata kunci metode steno atau
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
02 (bukan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
03, yang mengganggu penggantian nama properti). Getter tidak boleh mengubah status yang dapat diamati

Disallowed

/** @const */
exports = {exportedFunction};
6

5. 4. 8 Mengesampingkan keString

Metode

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_04 dapat diganti, tetapi harus selalu berhasil dan tidak pernah memiliki efek samping yang terlihat

Tip. Berhati-hatilah, khususnya, memanggil metode lain dari toString, karena kondisi luar biasa dapat menyebabkan perulangan tak terbatas

5. 4. 9 Antarmuka

Antarmuka dapat dideklarasikan dengan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_05 atau
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
06. Antarmuka yang dideklarasikan dengan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_06 dapat secara eksplisit (i. e. via
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_08) atau secara implisit diimplementasikan oleh kelas atau objek literal

Semua badan metode non-statis pada antarmuka harus berupa blok kosong. Bidang harus dideklarasikan sebagai anggota yang tidak diinisialisasi di kelas konstruktor

Example

/** @const */
exports = {exportedFunction};
7

5. 4. 10 Kelas Abstrak

Gunakan kelas abstrak bila perlu. Kelas dan metode abstrak harus dianotasi dengan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
09. Jangan gunakan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_10. Lihat kelas dan metode abstrak

5. 5 Fungsi

5. 5. 1 Fungsi tingkat atas

Fungsi tingkat atas dapat ditentukan langsung pada objek

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
92, atau dideklarasikan secara lokal dan diekspor secara opsional. Lihat lebih lanjut tentang ekspor

Contoh

/** @const */
exports = {exportedFunction};
8
/** @const */
exports = {exportedFunction};
9

5. 5. 2 Fungsi dan penutupan bersarang

Fungsi mungkin berisi definisi fungsi bersarang. Jika berguna untuk memberi nama fungsi, itu harus ditugaskan ke

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33 lokal

5. 5. 3 Fungsi panah

Fungsi panah menyediakan sintaks fungsi yang ringkas dan menyederhanakan pelingkupan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 untuk fungsi bersarang. Lebih suka fungsi panah daripada kata kunci
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_01, terutama untuk fungsi bersarang (tetapi lihat )

Lebih suka fungsi panah daripada

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_66 pendekatan pelingkupan lainnya seperti
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
16,
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
17, dan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
18. Fungsi panah sangat berguna untuk memanggil panggilan balik karena memungkinkan secara eksplisit menentukan parameter mana yang akan diteruskan ke panggilan balik sedangkan pengikatan akan secara membabi buta meneruskan semua parameter

Sisi kiri panah berisi nol atau lebih parameter. Tanda kurung di sekitar parameter bersifat opsional jika hanya ada satu parameter yang tidak dirusak. Saat tanda kurung digunakan, jenis parameter sebaris dapat ditentukan (lihat )

Tip. Always using parentheses even for single-parameter arrow functions can avoid situations where adding parameters, but forgetting to add parentheses, may result in parseable code which no longer works as intended

Sisi kanan panah berisi badan fungsi. Secara default badan adalah pernyataan blok (nol atau lebih pernyataan dikelilingi oleh kurung kurawal). Tubuh juga dapat berupa ekspresi tunggal yang dikembalikan secara implisit jika salah satunya. logika program memerlukan pengembalian nilai, atau operator

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
20 mendahului pemanggilan fungsi atau metode tunggal (menggunakan
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
20 memastikan
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
45 dikembalikan, mencegah kebocoran nilai, dan menyampaikan maksud). Bentuk ekspresi tunggal lebih disukai jika meningkatkan keterbacaan (mis. g. , untuk ekspresi pendek atau sederhana)

Contoh

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
0

Disallowed

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
1

5. 5. 4 Generator

Generator mengaktifkan sejumlah abstraksi yang berguna dan dapat digunakan sesuai kebutuhan

Saat mendefinisikan fungsi generator, lampirkan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
31 ke kata kunci
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
01 saat ada, dan pisahkan dengan spasi dari nama fungsi. Saat menggunakan pendelegasian hasil, lampirkan
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
31 ke kata kunci
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
26

Example

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
2

5. 5. 5 Parameter dan tipe pengembalian

Parameter fungsi dan tipe pengembalian biasanya harus didokumentasikan dengan anotasi JSDoc. Lihat untuk informasi lebih lanjut

5. 5. 5. 1 Parameter standar

Parameter opsional diizinkan menggunakan operator sama dengan dalam daftar parameter. Parameter opsional harus menyertakan spasi di kedua sisi operator sama dengan, diberi nama persis seperti parameter yang diperlukan (mis. e. , tidak diawali dengan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_26), gunakan akhiran
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
27 dalam jenis JSDoc mereka, muncul setelah parameter yang diperlukan, dan tidak menggunakan penginisialisasi yang menghasilkan efek samping yang dapat diamati. Semua parameter opsional untuk fungsi konkret harus memiliki nilai default, meskipun nilainya adalah
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
45. Berbeda dengan fungsi konkret, metode abstrak dan antarmuka harus menghilangkan nilai parameter default

Example

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
3

Gunakan parameter default dengan hemat. Lebih memilih destrukturisasi (seperti pada ) untuk membuat API yang dapat dibaca ketika ada lebih dari segelintir parameter opsional yang tidak memiliki tatanan alami

Catatan. Tidak seperti parameter default Python, boleh saja menggunakan penginisialisasi yang mengembalikan objek baru yang dapat diubah (seperti

goog.module('search.urlHistory.UrlHistoryService');
81 atau
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
52) karena penginisialisasi dievaluasi setiap kali nilai default digunakan, jadi satu objek tidak akan dibagikan di seluruh pemanggilan

Tip. Meskipun ekspresi arbitrer termasuk pemanggilan fungsi dapat digunakan sebagai penginisialisasi, ini harus dibuat sesederhana mungkin. Hindari penginisialisasi yang menampilkan status bersama yang dapat diubah, karena hal itu dapat dengan mudah menyebabkan penggabungan yang tidak disengaja di antara pemanggilan fungsi

5. 5. 5. 2 Parameter istirahat

Gunakan parameter istirahat alih-alih mengakses ________41______31. Parameter istirahat diketik dengan awalan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51 di JSDoc mereka. Parameter sisanya harus menjadi parameter terakhir dalam daftar. Tidak ada spasi antara
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51 dan nama parameter. Jangan beri nama parameter lainnya
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
34. Jangan pernah memberi nama variabel atau parameter lokal
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
31, yang membayangi nama bawaan secara membingungkan

Example

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
4

5. 5. 6 Generik

Deklarasikan fungsi dan metode generik bila perlu dengan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
36 di JSDoc di atas definisi fungsi atau metode

5. 5. 7 Menyebarkan operator

Panggilan fungsi dapat menggunakan operator sebar (

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51). Lebih suka operator sebar ke
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_38 saat array atau iterable dibongkar menjadi beberapa parameter fungsi variadik. Tidak ada spasi setelah
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51

Example

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
5

5. 6 string literal

5. 6. 1 Gunakan tanda kutip tunggal

Literal string biasa dibatasi dengan tanda kutip tunggal (

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
40), bukan tanda kutip ganda (
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
41)

Tip. jika string berisi karakter kutipan tunggal, pertimbangkan untuk menggunakan string template agar tidak perlu keluar dari kutipan

Literal string biasa mungkin tidak menjangkau beberapa baris

5. 6. 2 Template literal

Gunakan literal templat (dibatasi dengan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
42) melalui rangkaian string kompleks, terutama jika beberapa literal string terlibat. Literal template dapat menjangkau beberapa baris

Jika templat literal terdiri dari beberapa baris, ia tidak perlu mengikuti indentasi dari blok terlampir, meskipun mungkin jika penambahan spasi putih tidak menjadi masalah

Example

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
6

5. 6. 3 Tidak ada kelanjutan garis

Jangan gunakan kelanjutan baris (yaitu, mengakhiri baris di dalam literal string dengan garis miring terbalik) baik dalam literal string biasa maupun template. Meskipun ES5 memungkinkan ini, ini dapat menyebabkan kesalahan yang rumit jika ada spasi kosong setelah garis miring, dan kurang jelas bagi pembaca

Disallowed

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
7

Sebagai gantinya, tulislah

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
8

5. 7 Jumlah literal

Angka dapat ditentukan dalam desimal, hex, oktal, atau biner. Gunakan tepat

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
43,
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
44, dan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
45 awalan, dengan huruf kecil, masing-masing untuk hex, oktal, dan biner. Jangan pernah menyertakan awalan nol kecuali segera diikuti oleh
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
46,
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
47, atau
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
48

5. 8 Struktur kontrol

5. 8. 1 Untuk loop

Dengan ES6, bahasa sekarang memiliki tiga jenis

goog.module('search.urlHistory.UrlHistoryService');
75 loop yang berbeda. Semua dapat digunakan, meskipun
goog.module('search.urlHistory.UrlHistoryService');
_75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
25 loop harus lebih disukai bila memungkinkan

goog.module('search.urlHistory.UrlHistoryService');
75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
24 loop hanya dapat digunakan pada objek bergaya dict (lihat ), dan tidak boleh digunakan untuk mengulang array.
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_54 harus digunakan dalam
goog.module('search.urlHistory.UrlHistoryService');
75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
24 loop untuk mengecualikan properti prototipe yang tidak diinginkan. Lebih suka ________ 35 ________75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
25 dan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
59 daripada ________35 _______75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
24 jika memungkinkan

5. 8. 2 Pengecualian

Exceptions are an important part of the language and should be used whenever exceptional cases occur. Selalu buang

goog.module('search.urlHistory.UrlHistoryService');
_63s atau subkelas dari
goog.module('search.urlHistory.UrlHistoryService');
63. jangan pernah membuang string literal atau objek lain. Selalu gunakan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_64 saat membuat
goog.module('search.urlHistory.UrlHistoryService');
63

Perawatan ini meluas ke

goog.module('search.urlHistory.UrlHistoryService');
65 nilai penolakan karena
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
67 setara dengan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
68 dalam fungsi async

Pengecualian khusus memberikan cara yang bagus untuk menyampaikan informasi kesalahan tambahan dari fungsi. Mereka harus didefinisikan dan digunakan di mana pun jenis ________35______63 asli tidak mencukupi

Lebih suka melempar pengecualian daripada pendekatan penanganan kesalahan ad-hoc (seperti meneruskan jenis referensi wadah kesalahan, atau mengembalikan objek dengan properti kesalahan)

Sangat jarang benar untuk tidak melakukan apa-apa dalam menanggapi pengecualian yang tertangkap. Ketika benar-benar tepat untuk tidak mengambil tindakan apa pun dalam blok tangkap, alasan mengapa hal ini dibenarkan dijelaskan dalam komentar

import './sideeffects.js';

import * as goog from '../closure/goog/goog.js';
import * as parent from '../parent.js';

import {name} from './sibling.js';
_9

Disallowed

import '../directory/file';
_0

Tip. Tidak seperti di beberapa bahasa lain, pola seperti di atas tidak berfungsi karena ini akan menangkap kesalahan yang dilemparkan oleh

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
70. Gunakan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_71 sebagai gantinya

5. 8. 3 Tukar pernyataan

Catatan Terminologi. Di dalam kurung blok sakelar ada satu atau lebih grup pernyataan. Setiap kelompok pernyataan terdiri dari satu atau lebih label sakelar (baik

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
72 atau
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
73), diikuti oleh satu atau lebih pernyataan

5. 8. 3. 1 Gagal. berkomentar

Di dalam blok sakelar, setiap grup pernyataan berhenti secara tiba-tiba (dengan pengecualian ________35______89,

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
21 atau
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
22n), atau ditandai dengan komentar untuk menunjukkan bahwa eksekusi akan atau mungkin berlanjut ke grup pernyataan berikutnya. Setiap komentar yang mengomunikasikan ide fall-through sudah cukup (biasanya
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
77). Komentar khusus ini tidak diperlukan dalam grup pernyataan terakhir dari blok sakelar

Example

import '../directory/file';
_1
5. 8. 3. 2 Kasus
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_78 hadir

Setiap pernyataan switch menyertakan grup pernyataan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_78, meskipun tidak berisi kode. Grup pernyataan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_78 harus menjadi yang terakhir

5. 9 ini

Hanya gunakan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 dalam konstruktor dan metode kelas, dalam fungsi panah yang didefinisikan dalam konstruktor dan metode kelas, atau dalam fungsi yang memiliki
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
82 eksplisit yang dideklarasikan dalam JSDoc fungsi yang langsung dilampirkan

Jangan pernah menggunakan

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 untuk merujuk ke objek global, konteks ________41______84, target suatu peristiwa, atau fungsi
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
85ed atau
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
86ed yang tidak perlu

5. 10 Pemeriksaan Kesetaraan

Gunakan operator identitas (

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
87/________41______88) kecuali dalam kasus yang didokumentasikan di bawah

5. 10. 1 Pengecualian Dimana Pemaksaan Diinginkan

Menangkap nilai

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
89 dan
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
45

import '../directory/file';
_2

5. 11 Fitur yang dilarang

5. 11. 1 dengan

Jangan gunakan kata kunci

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_91. Itu membuat kode Anda lebih sulit dipahami dan telah dilarang dalam mode ketat sejak ES5

5. 11. 2 Evaluasi kode dinamis

Jangan gunakan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
84 atau
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
93 konstruktor (kecuali untuk pemuat kode). Fitur-fitur ini berpotensi berbahaya dan tidak berfungsi di lingkungan CSP

5. 11. 3 Penyisipan titik koma otomatis

Selalu hentikan pernyataan dengan titik koma (kecuali deklarasi fungsi dan kelas, seperti disebutkan di atas)

5. 11. 4 Fitur non-standar

Jangan gunakan fitur non-standar. Ini termasuk fitur lama yang telah dihapus (mis. g. ,

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
94), fitur baru yang belum dibakukan (mis. g. , draf kerja TC39 saat ini, proposal pada tahap apa pun, atau standar web yang diusulkan tetapi belum lengkap), atau fitur eksklusif yang hanya diterapkan di beberapa browser. Gunakan hanya fitur yang ditentukan dalam standar ECMA-262 atau WHATWG saat ini. (Perhatikan bahwa proyek menulis terhadap API tertentu, seperti ekstensi Chrome atau Node. js, jelas bisa menggunakan API tersebut). "Ekstensi" bahasa non-standar (seperti yang disediakan oleh beberapa transpiler eksternal) dilarang

5. 11. 5 Objek pembungkus untuk tipe primitif

Jangan pernah menggunakan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
64 pada pembungkus objek primitif (
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
96,
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
97,
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
98,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
85), atau menyertakannya dalam anotasi jenis

Disallowed

import '../directory/file';
_3

Pembungkus dapat disebut sebagai fungsi untuk memaksa (yang lebih disukai daripada menggunakan

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
00 atau menggabungkan string kosong) atau membuat simbol

Example

import '../directory/file';
_4

5. 11. 6 Memodifikasi objek bawaan

Jangan pernah memodifikasi tipe bawaan, baik dengan menambahkan metode ke konstruktornya atau ke prototipenya. Hindari bergantung pada perpustakaan yang melakukan ini. Perhatikan bahwa pustaka runtime JSCompiler akan menyediakan polyfill yang memenuhi standar jika memungkinkan;

Do not add symbols to the global object unless absolutely necessary (e. g. diperlukan oleh API pihak ketiga)

5. 11. 7 Menghilangkan
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_01 saat memanggil konstruktor

Jangan pernah memanggil konstruktor dalam pernyataan

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
64 tanpa menggunakan tanda kurung
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
01

Disallowed

import '../directory/file';
_5

Gunakan sebagai gantinya

import '../directory/file';
_6

Menghilangkan tanda kurung dapat menyebabkan kesalahan halus. Kedua baris ini tidak setara

import '../directory/file';
_7

6 Namun

6. 1 Aturan umum untuk semua pengidentifikasi

Pengidentifikasi hanya menggunakan huruf dan angka ASCII, dan, dalam sejumlah kecil kasus yang disebutkan di bawah, garis bawah dan sangat jarang (bila diperlukan oleh kerangka kerja seperti Angular) tanda dolar

Berikan nama yang sedeskriptif mungkin, dengan alasan. Jangan khawatir tentang menghemat ruang horizontal karena jauh lebih penting untuk membuat kode Anda segera dapat dipahami oleh pembaca baru. Jangan gunakan singkatan yang ambigu atau asing bagi pembaca di luar proyek Anda, dan jangan menyingkat dengan menghapus huruf di dalam kata

import '../directory/file';
_8

Disallowed

import '../directory/file';
_9

6. 2 Aturan menurut jenis pengidentifikasi

6. 2. 1 Nama paket

Nama paket semuanya

goog.module('search.urlHistory.UrlHistoryService');
_01. Misalnya,
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
05, tetapi bukan
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
06 atau
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
07

6. 2. 2 nama kelas

Nama class, interface, record, dan typedef ditulis dalam

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
08. Kelas yang tidak diekspor hanyalah penduduk lokal. mereka tidak ditandai
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 dan oleh karena itu tidak diberi nama dengan garis bawah di belakang

Jenis nama biasanya kata benda atau frase kata benda. Misalnya,

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
10,
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
11, atau
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
12. Selain itu, nama antarmuka terkadang dapat berupa kata sifat atau frasa kata sifat (misalnya,
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
13)

6. 2. 3 Nama metode

Nama metode ditulis dalam

goog.module('search.urlHistory.UrlHistoryService');
01. Nama untuk metode
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_94 harus diakhiri dengan garis bawah

Nama metode biasanya berupa kata kerja atau frase kata kerja. Misalnya,

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_16 atau
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
17. Metode pengambil dan penyetel untuk properti tidak pernah diperlukan, tetapi jika digunakan, metode tersebut harus diberi nama
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
18 (atau opsional
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
19 atau
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
20 untuk boolean), atau
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
21 untuk penyetel

Garis bawah juga dapat muncul dalam nama metode pengujian JsUnit untuk memisahkan komponen logis dari nama tersebut. Salah satu pola tipikal adalah

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_22, misalnya
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
23. Tidak ada Satu Cara yang Benar untuk menyebutkan metode pengujian

6. 2. 4 nama Enum

Nama Enum ditulis dalam

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_08, mirip dengan kelas, dan umumnya harus berupa kata benda tunggal. Masing-masing item dalam enum diberi nama di
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
25

6. 2. 5 nama tetap

Nama konstan menggunakan ________52______25. semua huruf besar, dengan kata-kata yang dipisahkan oleh garis bawah. Tidak ada alasan untuk sebuah konstanta diberi nama dengan tanda garis bawah, karena properti statis pribadi dapat diganti dengan lokal modul (secara implisit pribadi)

6. 2. 5. 1 Definisi "konstan"

Setiap konstanta adalah properti statis

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 atau deklarasi modul-lokal
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33, tetapi tidak semua properti statis
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 dan modul-lokal
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33 adalah konstanta. Sebelum memilih kasus konstanta, pertimbangkan apakah bidang tersebut benar-benar terasa seperti konstanta yang sangat tidak dapat diubah. Misalnya, jika salah satu keadaan yang dapat diamati dari contoh itu dapat berubah, hampir pasti itu bukan konstanta. Hanya berniat untuk tidak pernah memutasikan objek pada umumnya tidaklah cukup

Contoh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_00

Nama konstanta biasanya berupa kata benda atau frase kata benda

6. 2. 5. 2 alias lokal

Alias ​​​​lokal harus digunakan setiap kali mereka meningkatkan keterbacaan atas nama yang sepenuhnya memenuhi syarat. Ikuti aturan yang sama seperti

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_74s (), pertahankan bagian terakhir dari nama alias. Alias ​​​​juga dapat digunakan dalam fungsi. Alias ​​harus
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_33

Contoh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_01

6. 2. 6 Nama bidang non-konstan

Nama bidang non-konstan (statis atau lainnya) ditulis dalam

goog.module('search.urlHistory.UrlHistoryService');
01, dengan garis bawah di belakang untuk bidang pribadi

Nama-nama ini biasanya kata benda atau frase kata benda. Misalnya,

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_34 atau
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
35

6. 2. 7 Nama parameter

Nama parameter ditulis dalam

goog.module('search.urlHistory.UrlHistoryService');
_01. Perhatikan bahwa ini berlaku bahkan jika parameter mengharapkan konstruktor

Nama parameter satu karakter tidak boleh digunakan dalam metode publik

Pengecualian. Saat dibutuhkan oleh kerangka kerja pihak ketiga, nama parameter dapat diawali dengan

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
37. This exception does not apply to any other identifiers (e. g. variabel atau properti lokal)

6. 2. 8 Nama variabel lokal

Nama variabel lokal ditulis dalam

goog.module('search.urlHistory.UrlHistoryService');
_01, kecuali untuk konstanta modul-lokal (tingkat atas), seperti dijelaskan di atas. Konstanta dalam lingkup fungsi masih diberi nama di
goog.module('search.urlHistory.UrlHistoryService');
01. Perhatikan bahwa
goog.module('search.urlHistory.UrlHistoryService');
_01 digunakan bahkan jika variabel memiliki konstruktor

6. 2. 9 Nama parameter templat

Nama parameter template harus ringkas, pengidentifikasi satu kata atau satu huruf, dan harus huruf besar semua, seperti

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
41 atau
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
42

6. 2. 10 Nama modul-lokal

Nama modul-lokal yang tidak diekspor bersifat pribadi secara implisit. Mereka tidak ditandai

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_94 dan tidak diakhiri dengan garis bawah. Ini berlaku untuk kelas, fungsi, variabel, konstanta, enum, dan pengidentifikasi modul-lokal lainnya

6. 3 kotak unta. didefinisikan

Terkadang ada lebih dari satu cara yang masuk akal untuk mengonversi frasa bahasa Inggris menjadi huruf unta, seperti ketika ada akronim atau konstruksi yang tidak biasa seperti IPv6 atau iOS. Untuk meningkatkan prediktabilitas, Google Style menentukan skema deterministik (hampir) berikut

Diawali dengan bentuk prosa dari nama tersebut

  1. Ubah frasa menjadi ASCII biasa dan hapus apostrof apa pun. Misalnya, algoritme Müller mungkin menjadi algoritme Mueller
  2. Bagilah hasil ini menjadi kata-kata, pisahkan dengan spasi dan tanda baca yang tersisa (biasanya tanda hubung)
    1. Direkomendasikan. jika ada kata yang sudah memiliki tampilan kasing unta konvensional dalam penggunaan umum, pisahkan ini menjadi bagian penyusunnya (mis. g. , AdWords menjadi kata iklan). Perhatikan bahwa kata seperti iOS tidak benar-benar dalam bentuk unta per se;
  3. Sekarang huruf kecil semuanya (termasuk akronim), lalu huruf besar hanya karakter pertama
    1. .. setiap kata, untuk menghasilkan huruf unta atas, atau
    2. .. setiap kata kecuali yang pertama, untuk menghasilkan huruf unta yang lebih rendah
  4. Terakhir, gabungkan semua kata menjadi satu pengenal

Perhatikan bahwa huruf besar dari kata aslinya hampir seluruhnya diabaikan

Contoh

Prose formCorrectIncorrectXML HTTP requestXmlHttpRequestXMLHTTPRequestnew customer IDnewCustomerIdnewCustomerIDinner stopwatchinnerStopwatchinnerStopWatchsupports IPv6 on iOS?supportsIpv6OnIossupportsIPv6OnIOSYouTube importerYouTubeImporterYoutubeImporter*

* Dapat diterima, tetapi tidak direkomendasikan

Catatan. Beberapa kata ditulis dgn tanda penghubung secara ambigu dalam bahasa Inggris. misalnya tidak kosong dan tidak kosong keduanya benar, jadi nama metode checkNonempty dan checkNonEmpty juga benar

7 JSDok

JSDoc digunakan di semua kelas, bidang, dan metode

7. 1 Bentuk umum

Pemformatan dasar blok JSDoc seperti yang terlihat pada contoh ini

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_02

atau dalam contoh baris tunggal ini

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_03

Jika komentar satu baris meluap menjadi beberapa baris, itu harus menggunakan gaya multibaris dengan

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
44 dan
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
45 pada baris mereka sendiri

Banyak alat mengekstrak metadata dari komentar JSDoc untuk melakukan validasi dan pengoptimalan kode. Karena itu, komentar-komentar ini harus dibentuk dengan baik

7. 2 Penurunan harga

JSDoc ditulis dalam Markdown, meskipun mungkin menyertakan HTML jika diperlukan

Perhatikan bahwa alat yang secara otomatis mengekstrak JSDoc (mis. g. JsDossier) akan sering mengabaikan pemformatan teks biasa, jadi jika Anda melakukan ini

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_04

itu akan keluar seperti ini

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_05

Sebagai gantinya, tulis daftar Markdown

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_06

Gaya Google memungkinkan subset dari tag JSDoc. Lihat untuk daftar lengkapnya. Sebagian besar tag harus menempati barisnya sendiri, dengan tag di awal baris

Disallowed

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_07

Tag sederhana yang tidak memerlukan data tambahan apa pun (seperti

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96,
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
48,
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
49) dapat digabungkan ke baris yang sama, bersama dengan jenis opsional bila sesuai

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_08

Tidak ada aturan keras kapan harus menggabungkan tag, atau dalam urutan apa, tetapi konsistenlah

Untuk informasi umum tentang jenis anotasi dalam JavaScript, lihat Menganotasi JavaScript untuk Compiler Penutupan dan Jenis dalam Sistem Jenis Penutupan

7. 4 baris pembungkus

Tag blok yang dibungkus baris diberi indentasi empat spasi. Teks deskripsi yang dibungkus dapat disejajarkan dengan deskripsi pada baris sebelumnya, tetapi perataan horizontal ini tidak disarankan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_09

Jangan membuat indentasi saat membungkus deskripsi

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
50 atau
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
70

File mungkin memiliki ikhtisar file tingkat atas. Pemberitahuan hak cipta, informasi penulis, dan default bersifat opsional. Ikhtisar file umumnya direkomendasikan setiap kali file terdiri dari lebih dari satu definisi kelas. The top level comment is designed to orient readers unfamiliar with the code to what is in this file. Jika ada, ini dapat memberikan deskripsi konten file dan dependensi atau informasi kompatibilitas apa pun. Garis yang dibungkus tidak menjorok ke dalam

Example

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_10

Kelas, antarmuka, dan catatan harus didokumentasikan dengan deskripsi dan setiap parameter templat, antarmuka yang diimplementasikan, visibilitas, atau tag lain yang sesuai. Deskripsi kelas harus memberikan informasi yang cukup kepada pembaca untuk mengetahui bagaimana dan kapan menggunakan kelas, serta pertimbangan tambahan yang diperlukan untuk menggunakan kelas dengan benar. Deskripsi tekstual dapat dihilangkan pada konstruktor.

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_52 dan
goog.module('search.urlHistory.UrlHistoryService');
88 anotasi tidak digunakan dengan kata kunci
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
97 kecuali kelas digunakan untuk mendeklarasikan
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
05 atau memperluas kelas generik

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_11

Semua enum dan typedef harus didokumentasikan dengan tag JSDoc yang sesuai (

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
56 atau
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
73) pada baris sebelumnya. Enum dan typedef publik juga harus memiliki deskripsi. Item enum individu dapat didokumentasikan dengan komentar JSDoc pada baris sebelumnya

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_12

Typedef berguna untuk mendefinisikan tipe record pendek, atau alias untuk gabungan, fungsi kompleks, atau tipe generik. Typedefs harus dihindari untuk tipe record dengan banyak field, karena mereka tidak mengizinkan mendokumentasikan field individual, atau menggunakan template atau referensi rekursif. Untuk jenis rekaman besar, pilih

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
_06

Dalam metode dan fungsi bernama, parameter dan tipe pengembalian harus didokumentasikan, kecuali dalam kasus tanda tangan yang sama

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
59s, di mana semua tipe dihilangkan. Jenis
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
_66 harus didokumentasikan bila diperlukan. Jenis pengembalian dapat dihilangkan jika fungsi tidak memiliki pernyataan
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
21 yang tidak kosong

Deskripsi metode, parameter, dan pengembalian (tetapi bukan tipe) dapat dihilangkan jika terlihat jelas dari JSDoc metode lainnya atau dari tanda tangannya

Deskripsi metode dimulai dengan frase kata kerja yang menjelaskan apa yang dilakukan metode tersebut. Frasa ini bukan kalimat imperatif, melainkan ditulis dengan kata ganti orang ketiga, seolah-olah ada yang tersirat. sebelum itu

Jika suatu metode menimpa metode superclass, itu harus menyertakan anotasi

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
59. Metode yang diganti mewarisi semua anotasi JSDoc dari metode kelas super (termasuk anotasi visibilitas) dan harus dihilangkan dalam metode yang diganti. Namun, jika ada jenis yang disempurnakan dalam anotasi jenis, semua anotasi
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
63 dan
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
64 harus ditentukan secara eksplisit

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_13

Jika Anda hanya perlu mendokumentasikan param dan mengembalikan jenis fungsi, Anda dapat menggunakan JSDocs sebaris dalam tanda tangan fungsi secara opsional. JSDocs sebaris ini menentukan tipe return dan param tanpa tag

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_14

Jika Anda memerlukan deskripsi atau tag, gunakan satu komentar JSDoc di atas metode ini. Misalnya, metode yang mengembalikan nilai memerlukan tag

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
64

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
15
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
16

Dalam fungsi anonim, anotasi umumnya opsional. Jika inferensi tipe otomatis tidak mencukupi atau anotasi eksplisit meningkatkan keterbacaan, maka beri anotasi param dan kembalikan tipe seperti ini

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_17

Untuk ekspresi tipe fungsi, lihat

Jenis properti harus didokumentasikan. Deskripsi dapat dihilangkan untuk properti pribadi, jika name dan type memberikan dokumentasi yang cukup untuk memahami kode

Konstanta yang diekspor secara publik dikomentari dengan cara yang sama seperti properti

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_18

7. 10 Ketik anotasi

Jenis anotasi ditemukan pada

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_63,
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
64,
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
82, dan
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
69 tag, dan opsional pada
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96,
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
49, dan setiap tag visibilitas. Anotasi jenis yang dilampirkan ke tag JSDoc harus selalu diapit oleh kurung kurawal

7. 10. 1 Nullabilitas

Sistem tipe mendefinisikan pengubah

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
_72 dan
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
73 masing-masing untuk non-null dan nullable. Pengubah ini harus mendahului tipe

Pengubah nullability memiliki persyaratan berbeda untuk jenis yang berbeda, yang terbagi dalam dua kategori besar

  1. Ketik anotasi untuk primitif (
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    74,
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    75,
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    76,
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    77,
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    45,
    goog.module('my.test.helpers');
    goog.module.declareLegacyNamespace();
    goog.setTestOnly();
    
    89) dan literal (
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    80 dan
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    81) selalu tidak dapat dibatalkan secara default. Gunakan pengubah
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    73 untuk membuatnya dapat dibatalkan, tetapi abaikan
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    72 yang berlebihan
  2. Jenis referensi (umumnya, apa pun di
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    _08, termasuk
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    85) merujuk ke kelas, enum, catatan, atau typedef yang didefinisikan di tempat lain. Karena jenis ini mungkin atau mungkin tidak dapat dibatalkan, tidak mungkin untuk mengetahui dari namanya saja apakah itu dapat dibatalkan atau tidak. Selalu gunakan pengubah
    const /** !Array */ exportedArray = [1, 2, 3];
    
    const /** !Array */ moduleLocalArray = [4, 5, 6];
    
    /** @return {number} */
    function moduleLocalFunction() {
      return moduleLocalArray.length;
    }
    
    /** @return {number} */
    function exportedFunction() {
      return moduleLocalFunction() * 2;
    }
    
    exports = {exportedArray, exportedFunction};
    
    73 dan ________52______72 eksplisit untuk jenis ini untuk mencegah ambiguitas di situs penggunaan

Buruk

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_19

Bagus

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_20

7. 10. 2 Jenis Pemeran

Jika kompiler tidak secara akurat menyimpulkan jenis ekspresi, dan pernyataan berfungsi di goog. menegaskan tidak dapat memperbaikinya, dimungkinkan untuk mengencangkan jenis dengan menambahkan komentar anotasi jenis dan melampirkan ekspresi dalam tanda kurung. Perhatikan bahwa tanda kurung diperlukan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_21

7. 10. 3 Jenis Parameter Templat

Selalu tentukan parameter template. Dengan cara ini kompiler dapat melakukan pekerjaan yang lebih baik dan memudahkan pembaca untuk memahami apa yang dilakukan kode

Buruk

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
22

Bagus

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_23

Kasus ketika parameter templat tidak boleh digunakan

  • goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    50 digunakan untuk hierarki tipe dan bukan sebagai struktur seperti peta

7. 10. 4 Ekspresi tipe fungsi

Catatan Terminologi. ekspresi tipe fungsi mengacu pada anotasi tipe untuk tipe fungsi dengan kata kunci

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
01 dalam anotasi (lihat contoh di bawah)

Jika definisi fungsi diberikan, jangan gunakan ekspresi tipe fungsi. Tentukan parameter dan kembalikan jenis dengan

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
63 dan
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
64, atau dengan anotasi sebaris (lihat ). Ini termasuk fungsi anonim dan fungsi yang didefinisikan dan ditugaskan ke const (di mana fungsi jsdoc muncul di atas seluruh ekspresi penugasan)

Ekspresi tipe fungsi diperlukan, misalnya, di dalam

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
56,
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
63 atau
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
64. Gunakan juga untuk variabel atau properti dari tipe fungsi, jika tidak segera diinisialisasi dengan definisi fungsi

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_24

Saat menggunakan ekspresi tipe fungsi, selalu tentukan tipe pengembalian secara eksplisit. Kalau tidak, tipe pengembalian default tidak diketahui (

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
73), yang mengarah ke perilaku aneh dan tidak terduga, dan jarang yang sebenarnya diinginkan

Kesalahan tipe buruk, tetapi tidak ada peringatan yang diberikan

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_25

Bagus

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_26

7. 10. 5 Spasi kosong

Dalam anotasi jenis, satu spasi atau jeda baris diperlukan setelah setiap koma atau titik dua. Jeda baris tambahan dapat disisipkan untuk meningkatkan keterbacaan atau menghindari melebihi batas kolom. Istirahat ini harus dipilih dan diindentasi mengikuti pedoman yang berlaku (mis. g. Dan ). Tidak ada spasi lain yang diperbolehkan dalam anotasi jenis

Bagus

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
27

Buruk

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
28

7. 11 Visibility annotations

Anotasi visibilitas (

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_94,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
79,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
78) dapat ditentukan dalam blok
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
70, atau pada simbol atau properti yang diekspor. Jangan tentukan visibilitas untuk variabel lokal, baik di dalam fungsi atau di tingkat atas modul. All
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 names must end with an underscore

8 Kebijakan

8. 1 Issues unspecified by Google Style. Jadilah Konsisten

For any style question that isn't settled definitively by this specification, prefer to do what the other code in the same file is already doing. If that doesn't resolve the question, consider emulating the other files in the same package

8. 2 Compiler warnings

8. 2. 1 Use a standard warning set

As far as possible projects should use

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
01

8. 2. 2 How to handle a warning

Before doing anything, make sure you understand exactly what the warning is telling you. If you're not positive why a warning is appearing, ask for help

Once you understand the warning, attempt the following solutions in order

  1. First, fix it or work around it. Make a strong attempt to actually address the warning, or find another way to accomplish the task that avoids the situation entirely
  2. Otherwise, determine if it's a false alarm. If you are convinced that the warning is invalid and that the code is actually safe and correct, add a comment to convince the reader of this fact and apply the
    /** @const {number} */
    exports.CONSTANT_ONE = 1;
    
    /** @const {string} */
    exports.CONSTANT_TWO = 'Another constant';
    
    02 annotation
  3. Otherwise, leave a TODO comment. This is a last resort. If you do this, do not suppress the warning. The warning should be visible until it can be taken care of properly

8. 2. 3 Suppress a warning at the narrowest reasonable scope

Warnings are suppressed at the narrowest reasonable scope, usually that of a single local variable or very small method. Often a variable or method is extracted for that reason alone

Example

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
29

Even a large number of suppressions in a class is still better than blinding the entire class to this type of warning

Mark deprecated methods, classes or interfaces with

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
03 annotations. A deprecation comment must include simple, clear directions for people to fix their call sites

8. 4 Code not in Google Style

You will occasionally encounter files in your codebase that are not in proper Google Style. These may have come from an acquisition, or may have been written before Google Style took a position on some issue, or may be in non-Google Style for any other reason

8. 4. 1 Reformatting existing code

When updating the style of existing code, follow these guidelines

  1. It is not required to change all existing code to meet current style guidelines. Reformatting existing code is a trade-off between code churn and consistency. Style rules evolve over time and these kinds of tweaks to maintain compliance would create unnecessary churn. However, if significant changes are being made to a file it is expected that the file will be in Google Style
  2. Be careful not to allow opportunistic style fixes to muddle the focus of a CL. If you find yourself making a lot of style changes that aren’t critical to the central focus of a CL, promote those changes to a separate CL

8. 4. 2 Newly added code. use Google Style

Brand new files use Google Style, regardless of the style choices of other files in the same package

When adding new code to a file that is not in Google Style, reformatting the existing code first is recommended, subject to the advice in

If this reformatting is not done, then new code should be as consistent as possible with existing code in the same file, but must not violate the style guide

8. 5 Local style rules

Teams and projects may adopt additional style rules beyond those in this document, but must accept that cleanup changes may not abide by these additional rules, and must not block such cleanup changes due to violating any additional rules. Beware of excessive rules which serve no purpose. The style guide does not seek to define style in every possible scenario and neither should you

8. 6 Generated code. mostly exempt

Source code generated by the build process is not required to be in Google Style. However, any generated identifiers that will be referenced from hand-written source code must follow the naming requirements. As a special exception, such identifiers are allowed to contain underscores, which may help to avoid conflicts with hand-written identifiers

9 Appendices

9. 1 JSDoc tag reference

JSDoc serves multiple purposes in JavaScript. In addition to being used to generate documentation it is also used to control tooling. The best known are the Closure Compiler type annotations

9. 1. 1 Type annotations and other Closure Compiler annotations

Documentation for JSDoc used by the Closure Compiler is described in Annotating JavaScript for the Closure Compiler and Types in the Closure Type System

9. 1. 2 Documentation annotations

In addition to the JSDoc described in Annotating JavaScript for the Closure Compiler the following tags are common and well supported by various documentation generation tools (such as JsDossier) for purely documentation purposes

You may also see other types of JSDoc annotations in third-party code. These annotations appear in the JSDoc Toolkit Tag Reference but are not considered part of valid Google style

9. 1. 2. 1
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
04 or
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
05 - Not recommended

Not recommended

Syntax.

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
06

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
30

Documents the author of a file or the owner of a test, generally only used in the

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
70 comment. The
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
05 tag is used by the unit test dashboard to determine who owns the test results

9. 1. 2. 2
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
09

Syntax.

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
10

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
31

Indicates what bugs the given test function regression tests

Multiple bugs should each have their own

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
09 line, to make searching for regression tests as easy as possible

9. 1. 2. 3
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
12 - Deprecated. Do not use

Deprecated. Do not use. Use Markdown backticks instead

Syntax.

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
13

Historically,

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
14 was written as
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
15

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
32

Indicates that a term in a JSDoc description is code so it may be correctly formatted in generated documentation

9. 1. 2. 4
const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
50

Syntax.

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
17

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
33
9. 1. 2. 5
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
18

Syntax.

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
19

This tag is used to generate cross-reference links within generated documentation

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
34

Catatan sejarah. Tag @link juga telah digunakan untuk membuat tautan eksternal dalam dokumentasi yang dihasilkan. Untuk tautan eksternal, selalu gunakan sintaks tautan Markdown

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_35
9. 1. 2. 6
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
20

Sintaksis.

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
_21

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_36

Referensi pencarian ke fungsi atau metode kelas lain

9. 1. 2. 7
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
_22

Sintaksis.

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
_23

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
_37

Digunakan dalam ikhtisar file untuk menunjukkan browser apa yang didukung oleh file

9. 1. 3 Framework specific annotations

The following annotations are specific to a particular framework

9. 1. 3. 1
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
24 for Angular 1
9. 1. 3. 2
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
25 for Polymer

https. //github. com/google/closure-compiler/wiki/Polymer-Pass

9. 1. 4 Notes about standard Closure Compiler annotations

The following tags used to be standard but are now deprecated

9. 1. 4. 1
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
26 - Deprecated. Do not use

Deprecated. Do not use. Use

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
49 and/or
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
87 instead

9. 1. 4. 2
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
29 - Deprecated. Do not use

Deprecated. Do not use. Use

const /** !Array */ exportedArray = [1, 2, 3];

const /** !Array */ moduleLocalArray = [4, 5, 6];

/** @return {number} */
function moduleLocalFunction() {
  return moduleLocalArray.length;
}

/** @return {number} */
function exportedFunction() {
  return moduleLocalFunction() * 2;
}

exports = {exportedArray, exportedFunction};
59 instead

9. 2 Commonly misunderstood style rules

Here is a collection of lesser-known or commonly misunderstood facts about Google Style for JavaScript. (The following are true statements; this is not a list of myths. )

  • Neither a copyright statement nor
    /** @const {number} */
    exports.CONSTANT_ONE = 1;
    
    /** @const {string} */
    exports.CONSTANT_TWO = 'Another constant';
    
    04 credit is required in a source file. (Neither is explicitly recommended, either. )
  • There is no hard and fast rule governing how to order the members of a class ()
  • Empty blocks can usually be represented concisely as
    goog.module('search.urlHistory.UrlHistoryService');
    
    81, as detailed in ()
  • The prime directive of line-wrapping is. prefer to break at a higher syntactic level ()
  • Non-ASCII characters are allowed in string literals, comments and JSDoc, and in fact are recommended when they make the code easier to read than the equivalent Unicode escape would ()

The following tools exist to support various aspects of Google Style

9. 3. 1 Closure Compiler

This program performs type checking and other checks, optimizations and other transformations (such as ECMAScript 6 to ECMAScript 5 code lowering)

9. 3. 2
goog.module('search.urlHistory.UrlHistoryService');
71

This program reformats JavaScript source code into Google Style, and also follows a number of non-required but frequently readability-enhancing formatting practices. The output produced by

goog.module('search.urlHistory.UrlHistoryService');
71 is compliant with the style guide

goog.module('search.urlHistory.UrlHistoryService');
71 is not required. Penulis diperbolehkan untuk mengubah keluarannya, dan peninjau diperbolehkan untuk meminta perubahan tersebut; . However, subtrees may choose to opt in to such enforcement locally

9. 3. 3 Closure compiler linter

This program checks for a variety of missteps and anti-patterns

9. 3. 4 Conformance framework

The JS Conformance Framework is a tool that is part of the Closure Compiler that provides developers a simple means to specify a set of additional checks to be run against their code base above the standard checks. Conformance checks can, for example, forbid access to a certain property, or calls to a certain function, or missing type information (unknowns)

These rules are commonly used to enforce critical restrictions (such as defining globals, which could break the codebase) and security patterns (such as using

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
84 or assigning to
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
37), or more loosely to improve code quality

For additional information see the official documentation for the JS Conformance Framework

9. 4 Exceptions for legacy platforms

9. 4. 1 Overview

This section describes exceptions and additional rules to be followed when modern ECMAScript 6 syntax is not available to the code authors. Exceptions to the recommended style are required when ECMAScript 6 syntax is not possible and are outlined here

  • Use of
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    37 declarations is allowed
  • Use of
    goog.module('my.test.helpers');
    goog.module.declareLegacyNamespace();
    goog.setTestOnly();
    
    31 is allowed
  • Optional parameters without default values are allowed

9. 4. 2 Use
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37

9. 4. 2. 1
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37 declarations are NOT block-scoped

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37 declarations are scoped to the beginning of the nearest enclosing function, script or module, which can cause unexpected behavior, especially with function closures that reference
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37 declarations inside of loops. The following code gives an example

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
38
9. 4. 2. 2 Declare variables as close as possible to first use

Even though

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37 declarations are scoped to the beginning of the enclosing function,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37 declarations should be as close as possible to their first use, for readability purposes. However, do not put a
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37 declaration inside a block if that variable is referenced outside the block. For example

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
39
9. 4. 2. 3 Use @const for constants variables

For global declarations where the

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33 keyword would be used, if it were available, annotate the
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37 declaration with @const instead (this is optional for local variables)

9. 4. 3 Do not use block scoped functions declarations

Do not do this

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
40

While most JavaScript VMs implemented before ECMAScript 6 support function declarations within blocks it was not standardized. Implementations were inconsistent with each other and with the now-standard ECMAScript 6 behavior for block scoped function declaration. ECMAScript 5 and prior only allow for function declarations in the root statement list of a script or function and explicitly ban them in block scopes in strict mode

To get consistent behavior, instead use a

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37 initialized with a function expression to define a function within a block

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
41

9. 4. 4 Dependency management with
goog.module('search.urlHistory.UrlHistoryService');
18/
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74

9. 4. 4. 1 Summary

WARNING.

goog.module('search.urlHistory.UrlHistoryService');
18 dependency management is deprecated. All new files, even in projects using
goog.module('search.urlHistory.UrlHistoryService');
18 for older files, should use . The following rules are for pre-existing
goog.module('search.urlHistory.UrlHistoryService');
18 files only

  • Place all
    goog.module('search.urlHistory.UrlHistoryService');
    
    18s first,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    74s second. Separate provides from requires with an empty line
  • Sort the entries alphabetically (uppercase first)
  • Don't wrap
    goog.module('search.urlHistory.UrlHistoryService');
    
    18 and
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    74 statements. Exceed 80 columns if necessary
  • Only provide top-level symbols

goog.module('search.urlHistory.UrlHistoryService');
18 statements should be grouped together and placed first. All
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 statements should follow. The two lists should be separated with an empty line

Similar to import statements in other languages,

goog.module('search.urlHistory.UrlHistoryService');
18 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 statements should be written in a single line, even if they exceed the 80 column line length limit

The lines should be sorted alphabetically, with uppercase letters coming first

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
42

All members defined on a class should be in the same file. Only top-level classes should be provided in a file that contains multiple members defined on the same class (e. g. enums, inner classes, etc)

Do this

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
43

Not this

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
44

Members on namespaces may also be provided

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
45
9. 4. 4. 2 Aliasing with
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
64

PERINGATAN.

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
64 is deprecated. New files should not use
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
64 even in projects with existing
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
64 usage

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
64 may be used to shorten references to namespaced symbols in code using
goog.module('search.urlHistory.UrlHistoryService');
18/
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 dependency management

Hanya satu permintaan

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
_64 yang dapat ditambahkan per file. Always place it in the global scope

The opening

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
72 invocation must be preceded by exactly one blank line and follow any
goog.module('search.urlHistory.UrlHistoryService');
18 statements,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 statements, or top-level comments. The invocation must be closed on the last line in the file. Append
/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
75 to the closing statement of the scope. Separate the comment from the semicolon by two spaces

Similar to C++ namespaces, do not indent under

/** @const {number} */
exports.CONSTANT_ONE = 1;

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
64 declarations. Instead, continue from the 0 column

Only make aliases for names that will not be re-assigned to another object (e. g. , most constructors, enums, and namespaces). Do not do this (see below for how to alias a constructor)

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
46

Names must be the same as the last property of the global that they are aliasing

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
47
9. 4. 4. 3
goog.module('search.urlHistory.UrlHistoryService');
25

Prefer to use

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 instead of
goog.module('search.urlHistory.UrlHistoryService');
25 to break circular dependencies between files in the same library. Unlike
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74, a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 statement is allowed to import a namespace before it is defined

goog.module('search.urlHistory.UrlHistoryService');
25 may still be used in legacy code to break circular references spanning across library boundaries, but newer code should be structured to avoid it

goog.module('search.urlHistory.UrlHistoryService');
25 statements must follow the same style rules as
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75. The entire block of
goog.module('search.urlHistory.UrlHistoryService');
25,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 statements is sorted alphabetically

Apa yang bisa dilakukan dengan JavaScript?

Javascript bisa digunakan untuk membuat fitur beragam seperti drag, drop komponen yang semuanya bisa bermanfaat untuk meningkatkan tampilan (interface) dan pengalaman menggunakan web . Selain itu, programmer juga bisa memperluas fungsi halaman web dengan menulis snippet Javascript untuk add-on pihak ketiga, contohnya .

Berapa gaji pengembang Menurut ID Neuvoo?

Menurut data dari laman Neuvoo, rata – rata seorang web developer di Amerika mencapai US$ 63,254 /per tahun atau US$ 5,271 / per bulannya. Sedangkan, untuk rata – rata gaji pengembang website di Indonesia berkisar antara 5 – 12 juta rupiah , menyesuaikan dengan UMR (Upah Minimum Regional) setiap daerah.

Apa itu JavaScript dan fungsinya?

JavaScript adalah bahasa pemrograman yang banyak digunakan dalam pengembangan website, aplikasi, dan game. Dengan menguasai bahasa pemrograman ini, Anda bisa membuat tampilan website yang menarik atau mengembangkan game online berbasis web yang populer.

Apakah W3SCHOOL?

W3SCOOL adalah sebuah situs untuk belajar programming terutama sekali sangat cocok buat kalian yang ingin belajar codin secara otodidak. Di W3SCHOOL kalian dapat belajar HTML, CSS, Javascript dan PHP mereka semua adalah bahasa bahasa web yang sangat cocok bahkan sangat baik untuk blogging.