In số nguyên tố javascript

Learn from over 300 examples. Với trình chỉnh sửa của chúng tôi, bạn có thể chỉnh sửa CSS và nhấp vào nút để xem kết quả

Show

Chuyển đến ví dụ về CSS


Sử dụng thực đơn

Chúng tôi khuyên bạn nên đọc hướng dẫn này, theo trình tự được liệt kê trong menu

Nếu bạn có màn hình lớn, menu sẽ luôn ở bên trái

Nếu bạn có màn hình nhỏ, hãy mở menu bằng cách nhấp vào ký hiệu menu trên cùng ☰


Mẫu CSS

Chúng tôi đã tạo một số W3 đáp ứng. Các mẫu CSS để bạn sử dụng

Bạn có thể tự do sửa đổi, lưu, chia sẻ và sử dụng chúng trong tất cả các dự án của mình

Mẫu CSS miễn phí



CSS Exercises

Test Yourself With Exercises


CSS Quiz

Test your CSS skills with a quiz

Start CSS Quiz


My Learning

Track your progress with the free "My Learning" program here at W3Schools

Log in to your account, and start earning points

This is an optional feature. You can study W3Schools without using My Learning

In số nguyên tố javascript


CSS References

At W3Schools you will find complete CSS references of all properties and selectors with syntax, examples, browser support, and more

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

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 Thông tin cơ bản về tệp nguồn

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 ký tự đặc biệt

2. 3. 1 Ký tự khoảng trắng

Ngoài trình tự kết thúc dòng, ký tự khoảng cách ngang ASCII (0x20) là ký tự khoảng trắng duy nhất xuất hiện ở bất kỳ đâu trong tệp nguồn. Điều này ngụ ý rằng

  1. Tất cả các ký tự khoảng trắng khác trong chuỗi ký tự được thoát và

  2. Các ký tự tab không được sử dụng để thụt lề

2. 3. 2 Trình tự thoát hiểm đặc biệt

Đối với bất kỳ ký tự nào có trình tự thoát đặc biệt (_______0_______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), trình tự đó được sử dụng thay vì trình tự thoát số tương ứng (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, hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
63). Thoát bát phân kế thừa không bao giờ được sử dụng

2. 3. 3 ký tự không phải ASCII

Đối với các ký tự không phải ASCII còn lại, ký tự Unicode thực tế (e. g.

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
64) hoặc bộ thoát hex hoặc Unicode tương đương (e. g.
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
65) được sử dụng, chỉ phụ thuộc vào cái nào làm cho mã dễ đọc và dễ hiểu hơn

Mẹo. Trong trường hợp thoát Unicode và đôi khi ngay cả khi các ký tự Unicode thực được sử dụng, một nhận xét giải thích có thể rất hữu ích

/* 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';

Mẹo. Đừng bao giờ làm cho mã của bạn khó đọc hơn chỉ vì sợ rằng một số chương trình có thể không xử lý đúng các ký tự không phải ASCII. If that happens, those programs are broken and they must be fixed

3 Cấu trúc tệp nguồn

Tất cả các tệp nguồn mới phải là tệp

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 (tệp chứa lệnh gọi
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66) hoặc mô-đun ECMAScript (ES) (sử dụng câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
68 và ________0____69). Các tập tin bao gồm những điều sau đây, theo thứ tự

  1. Thông tin giấy phép hoặc bản quyền, nếu có
  2. /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    70 JSDoc, nếu có
  3. Câu lệnh
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    66, nếu tệp
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    66
  4. ES
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    68 câu lệnh, nếu một mô-đun ES
  5. Câu lệnh
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    74 và
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    75
  6. Việc triển khai tệp

Chính xác một dòng trống phân tách từng phần hiện có, ngoại trừ việc triển khai tệp, có thể có 1 hoặc 2 dòng trống trước

3. 1 Giấy phép hoặc thông tin bản quyền, nếu có

Nếu thông tin giấy phép hoặc bản quyền thuộc về một tệp, thì nó thuộc về đây

3. 2 /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 70 JSDoc, nếu có

Xem các quy tắc định dạng

3. 3 /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 66 tuyên bố

Tất cả các tệp

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 phải khai báo chính xác một tên
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 trên một dòng. các dòng chứa khai báo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 không được ngắt dòng và do đó là một ngoại lệ đối với giới hạn 80 cột

Toàn bộ đối số với google. mô-đun là những gì xác định một không gian tên. Đó là tên gói (một mã định danh phản ánh đoạn cấu trúc thư mục chứa mã) cộng với, tùy chọn, lớp/enum/giao diện chính mà nó xác định được nối vào cuối

Thí dụ

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

3. 3. 1 thứ bậc

Không gian tên mô-đun không bao giờ được đặt tên là con trực tiếp của không gian tên mô-đun khác

không được phép

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

Hệ thống phân cấp thư mục phản ánh hệ thống phân cấp không gian tên, sao cho các thư mục con được lồng sâu hơn là thư mục con của thư mục mẹ cấp cao hơn. Lưu ý rằng điều này ngụ ý rằng chủ sở hữu của các nhóm không gian tên "cha mẹ" nhất thiết phải biết tất cả các không gian tên con, vì chúng tồn tại trong cùng một thư mục

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

Câu lệnh duy nhất

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 có thể tùy chọn được theo sau bởi lệnh gọi tới
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
83. Tránh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
84 khi có thể

Thí dụ

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
81 tồn tại để dễ dàng chuyển đổi từ các không gian tên dựa trên phân cấp đối tượng truyền thống nhưng đi kèm với một số hạn chế đặt tên. Vì tên mô-đun con phải được tạo sau không gian tên cha, tên này không được là con hoặc cha của bất kỳ
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 nào khác (ví dụ:
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
87 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
88 không thể tồn tại an toàn, cũng như
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
87 và
/* 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 Xuất khẩu

Các lớp, enum, hàm, hằng số và các ký hiệu khác được xuất bằng cách sử dụng đối tượng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
92. Các biểu tượng đã xuất có thể được xác định trực tiếp trên đối tượng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
92 hoặc được khai báo cục bộ và được xuất riêng. Các biểu tượng chỉ được xuất nếu chúng được sử dụng bên ngoài mô-đun. Các ký hiệu mô-đun-cục bộ không được xuất khẩu không được khai báo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 cũng như tên của chúng không kết thúc bằng dấu gạch dưới. Không có thứ tự theo quy định cho các ký hiệu được xuất và mô-đun-cục bộ

ví dụ

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

Không chú thích đối tượng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
92 là
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 vì nó đã được trình biên dịch coi là hằng số

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

3. 4 mô-đun ES

3. 4. 1 Nhập khẩu

Báo cáo nhập khẩu không được ngắt dòng và do đó là một ngoại lệ đối với giới hạn 80 cột

3. 4. 1. 1 Đường dẫn nhập

Các tệp mô-đun ES phải sử dụng câu lệnh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
68 để nhập các tệp mô-đun ES khác. Đừng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 mô-đun ES khác

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 Phần mở rộng tệp trong đường dẫn nhập

Phần mở rộng tệp

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
51 không phải là tùy chọn trong đường dẫn nhập và phải luôn được đưa vào

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

Không nhập cùng một tệp nhiều lần. Điều này có thể gây khó khăn cho việc xác định số lần nhập tổng hợp của một tệp

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
1
3. 4. 1. 3 Naming imports
3. 4. 1. 3. 1 Nhập mô-đun đặt tên

Tên nhập mô-đun (

goog.module('search.urlHistory.UrlHistoryService');
00) là tên
goog.module('search.urlHistory.UrlHistoryService');
01 được lấy từ tên tệp đã nhập

/* 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 Đặt tên nhập khẩu mặc định

Tên nhập mặc định được lấy từ tên tệp đã nhập và tuân theo các quy tắc trong

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

Ghi chú. Nói chung, điều này sẽ không xảy ra vì hướng dẫn kiểu này cấm xuất mặc định, xem. Nhập mặc định chỉ được sử dụng để nhập các mô-đun không tuân theo hướng dẫn kiểu này

3. 4. 1. 3. 3 Đặt tên cho hàng nhập đã đặt tên

Nói chung, các ký hiệu được nhập thông qua quá trình nhập có tên (______35_______02) nên giữ nguyên tên. Tránh nhập bí danh (

goog.module('search.urlHistory.UrlHistoryService');
03). Thích sửa xung đột tên bằng cách sử dụng mô-đun nhập (_______35_______04) hoặc tự đổi tên bản xuất

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

Nếu cần đổi tên một mục nhập đã đặt tên thì hãy sử dụng các thành phần của tên tệp hoặc đường dẫn của mô-đun đã nhập trong bí danh kết quả

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

3. 4. 2 xuất khẩu

Các biểu tượng chỉ được xuất nếu chúng được sử dụng bên ngoài mô-đun. Các ký hiệu mô-đun-cục bộ không được xuất khẩu không được khai báo

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 cũng như tên của chúng không kết thúc bằng dấu gạch dưới. Không có thứ tự theo quy định cho các ký hiệu được xuất và mô-đun-cục bộ

3. 4. 2. 1 Xuất khẩu được đặt tên và mặc định

Sử dụng xuất khẩu có tên trong tất cả các mã. Bạn có thể áp dụng từ khóa

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
69 cho khai báo hoặc sử dụng cú pháp
goog.module('search.urlHistory.UrlHistoryService');
07

Không sử dụng xuất khẩu mặc định. Nhập mô-đun phải đặt tên cho các giá trị này, điều này có thể dẫn đến sự không nhất quán trong việc đặt tên giữa các mô-đun

/* 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 Xuất các lớp và đối tượng vùng chứa tĩnh

Không xuất các lớp hoặc đối tượng vùng chứa bằng các phương thức hoặc thuộc tính tĩnh vì mục đích tạo không gian tên

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

Thay vào đó, hãy xuất các hằng số và hàm riêng lẻ

goog.module('search.urlHistory.UrlHistoryService');
1
3. 4. 2. 3 Tính biến động của hàng xuất khẩu

Các biến đã xuất không được thay đổi bên ngoài quá trình khởi tạo mô-đun

Có các lựa chọn thay thế nếu cần thay đổi, bao gồm xuất một tham chiếu không đổi đến một đối tượng có các trường có thể thay đổi hoặc xuất các hàm truy cập cho dữ liệu có thể thay đổi

goog.module('search.urlHistory.UrlHistoryService');
2
goog.module('search.urlHistory.UrlHistoryService');
3
3. 4. 2. 4 xuất khẩu từ

Các câu lệnh

goog.module('search.urlHistory.UrlHistoryService');
08 không được ngắt dòng và do đó là một ngoại lệ đối với giới hạn 80 cột. Điều này áp dụng cho cả hương vị
goog.module('search.urlHistory.UrlHistoryService');
08

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

3. 4. 3 vòng phụ thuộc trong các mô-đun ES

Không tạo chu kỳ giữa các mô-đun ES, mặc dù đặc tả ECMAScript cho phép điều này. Lưu ý rằng có thể tạo chu trình bằng cả câu lệnh

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

goog.module('search.urlHistory.UrlHistoryService');
5
goog.module('search.urlHistory.UrlHistoryService');
6
goog.module('search.urlHistory.UrlHistoryService');
7

3. 4. 4 Tương tác với Đóng cửa

3. 4. 4. 1 Tham khảo google

Để tham chiếu không gian tên Closure

goog.module('search.urlHistory.UrlHistoryService');
12, nhập Closure's
goog.module('search.urlHistory.UrlHistoryService');
13

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

goog.module('search.urlHistory.UrlHistoryService');
13 chỉ xuất một tập hợp con các thuộc tính từ
goog.module('search.urlHistory.UrlHistoryService');
12 toàn cầu có thể được sử dụng trong các mô-đun ES

3. 4. 4. 2 tốt. yêu cầu trong các mô-đun ES

Các mô-đun

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 trong ES hoạt động giống như trong các tệp
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66. Bạn có thể yêu cầu bất kỳ ký hiệu không gian tên Closure nào (i. e. , symbols created by
goog.module('search.urlHistory.UrlHistoryService');
18 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66) and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 will return the value

goog.module('search.urlHistory.UrlHistoryService');
9
3. 4. 4. 3 Khai báo ID mô-đun đóng trong mô-đun ES

Có thể sử dụng

goog.module('search.urlHistory.UrlHistoryService');
21 trong các mô-đun ES để khai báo ID mô-đun giống như
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66. Điều này có nghĩa là ID mô-đun này có thể là
/* 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, v.v. như thể đó là một
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 không gọi cho
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
81. Nó không tạo ID mô-đun dưới dạng biểu tượng JavaScript có sẵn trên toàn cầu

Một

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 (hoặc
goog.module('search.urlHistory.UrlHistoryService');
24) cho ID mô-đun từ
goog.module('search.urlHistory.UrlHistoryService');
21 sẽ luôn trả về đối tượng mô-đun (như thể nó là
goog.module('search.urlHistory.UrlHistoryService');
04'd). Kết quả là, đối số của
goog.module('search.urlHistory.UrlHistoryService');
21 phải luôn kết thúc bằng ____35_______33

Ghi chú. Có lỗi khi gọi

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
81 trong mô-đun ES, nó chỉ có thể được gọi từ tệp
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66. Không có cách nào trực tiếp để liên kết một không gian tên kế thừa với một mô-đun ES

Chỉ nên sử dụng

goog.module('search.urlHistory.UrlHistoryService');
21 để nâng cấp tệp Đóng lên mô-đun ES tại chỗ, nơi sử dụng xuất khẩu có tên

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

Trong tệp

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66, câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 có thể tùy chọn được theo sau bởi lệnh gọi tới
goog.module('search.urlHistory.UrlHistoryService');
40

Trong một mô-đun ES, các câu lệnh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
68 có thể tùy chọn được theo sau bởi một cuộc gọi tới
goog.module('search.urlHistory.UrlHistoryService');
40

3. 6 câu lệnh /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 74 và /* Poor: the reader has no idea what character this is. */ const units = '\u03bcs'; 75

Nhập khẩu được thực hiện với câu lệnh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75. Các tên được nhập bởi câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 có thể được sử dụng cả trong mã và chú thích loại, trong khi những tên được nhập bởi một
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 chỉ có thể được sử dụng trong chú thích loại

Các câu lệnh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 tạo thành một khối liền kề không có dòng trống. Khối này theo tuyên bố
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 được tách ra. Toàn bộ đối số của
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 là một không gian tên được xác định bởi một
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 trong một tệp riêng biệt. Các câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 không được xuất hiện ở bất kỳ nơi nào khác trong tệp

Mỗi

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 được gán cho một bí danh cố định duy nhất hoặc nếu không sẽ bị hủy cấu trúc thành một số bí danh cố định. Các bí danh này là cách duy nhất được chấp nhận để chỉ các phụ thuộc trong chú thích loại hoặc mã. Không được sử dụng các không gian tên đầy đủ ở bất kỳ đâu, ngoại trừ làm đối số cho
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75

Ngoại lệ. Các loại, biến và hàm được khai báo trong tệp bên ngoài phải sử dụng tên đủ điều kiện của chúng trong chú thích loại và mã

Bí danh phải khớp với thành phần cuối cùng được phân tách bằng dấu chấm trong không gian tên của mô-đun đã nhập

Ngoại lệ. Trong một số trường hợp nhất định, các thành phần bổ sung của không gian tên có thể được sử dụng để tạo bí danh dài hơn. Bí danh kết quả phải giữ lại cách viết hoa của mã định danh ban đầu để nó vẫn xác định chính xác loại của nó. Các bí danh dài hơn có thể được sử dụng để phân biệt các bí danh giống hệt nhau hoặc nếu nó cải thiện đáng kể khả năng đọc. Ngoài ra, phải sử dụng một bí danh dài hơn để ngăn che giấu các loại gốc như

goog.module('search.urlHistory.UrlHistoryService');
61,
goog.module('search.urlHistory.UrlHistoryService');
62,
goog.module('search.urlHistory.UrlHistoryService');
63,
goog.module('search.urlHistory.UrlHistoryService');
64 và
goog.module('search.urlHistory.UrlHistoryService');
65 (để biết danh sách đầy đủ hơn, hãy xem API Web và Đối tượng tích hợp sẵn tiêu chuẩn tại MDN). Khi đổi tên các bí danh bị hủy cấu trúc, một khoảng trắng phải theo sau dấu hai chấm theo yêu cầu trong

Một tệp không được chứa cả câu lệnh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 và câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 cho cùng một không gian tên. Nếu tên đã nhập được sử dụng cả trong chú thích mã và loại, thì tên đó phải được nhập bằng một câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74

Nếu một mô-đun được nhập chỉ vì tác dụng phụ của nó, lệnh gọi phải là __________74 (không phải __________75) và phép gán có thể bị bỏ qua. Cần có một bình luận để giải thích lý do tại sao điều này là cần thiết và loại bỏ cảnh báo của trình biên dịch

Các dòng được sắp xếp theo các quy tắc sau. Tất cả các yêu cầu có tên ở phía bên trái được ưu tiên trước, được sắp xếp theo thứ tự bảng chữ cái của các tên đó. Sau đó, yêu cầu phá hủy, được sắp xếp lại theo tên ở phía bên trái. Cuối cùng, bất kỳ cuộc gọi yêu cầu nào độc lập (thường là những cuộc gọi này dành cho các mô-đun được nhập chỉ vì tác dụng phụ của chúng)

Mẹo. Không cần phải ghi nhớ lệnh này và thực thi thủ công. Bạn có thể dựa vào IDE của mình để báo cáo các yêu cầu không được sắp xếp chính xác

Nếu một bí danh hoặc tên mô-đun dài sẽ khiến một dòng vượt quá giới hạn 80 cột, thì nó không được ngắt dòng. các dòng yêu cầu là một ngoại lệ đối với giới hạn 80 cột

Thí dụ

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

nản lòng

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

không được phép

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

3. 7 Việc triển khai tệp

Việc triển khai thực tế diễn ra sau khi tất cả thông tin phụ thuộc được khai báo (cách nhau ít nhất một dòng trống)

Điều này có thể bao gồm bất kỳ khai báo mô-đun-cục bộ nào (hằng, biến, lớp, hàm, v.v.), cũng như bất kỳ biểu tượng được xuất nào

4 Định dạng

Lưu ý thuật ngữ. cấu trúc giống như khối đề cập đến phần thân của một lớp, hàm, phương thức hoặc khối mã được phân cách bằng dấu ngoặc nhọn. Note that, by and , any array or object literal may optionally be treated as if it were a block-like construct

Mẹo. Sử dụng

goog.module('search.urlHistory.UrlHistoryService');
71. Cộng đồng JavaScript đã đầu tư nỗ lực để đảm bảo clang-format hoạt động đúng trên các tệp JavaScript.
goog.module('search.urlHistory.UrlHistoryService');
71 có tích hợp với một số trình soạn thảo phổ biến

4. 1 niềng răng

4. 1. 1 Niềng răng được sử dụng cho tất cả các cấu trúc điều khiển

Niềng răng được yêu cầu cho tất cả các cấu trúc điều khiển (i. 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, cũng như bất kỳ câu lệnh nào khác), ngay cả khi phần nội dung chỉ chứa một câu lệnh duy nhất. Câu lệnh đầu tiên của một khối không trống phải bắt đầu trên dòng của chính nó

không được phép

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

Ngoại lệ. Một câu lệnh if đơn giản có thể vừa khít hoàn toàn trên một dòng mà không có dòng xuống dòng (và không có dòng nào khác) có thể được giữ trên một dòng không có dấu ngoặc khi nó cải thiện khả năng đọc. Đây là trường hợp duy nhất trong đó cấu trúc điều khiển có thể bỏ dấu ngoặc nhọn và dòng mới

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

4. 1. 2 khối rỗng. phong cách K&R

Dấu ngoặc theo kiểu Kernighan và Ritchie (dấu ngoặc của người Ai Cập) cho các khối không trống và các cấu trúc giống như khối

  • Không ngắt dòng trước dấu ngoặc mở
  • Ngắt dòng sau cú đúp mở đầu
  • Ngắt dòng trước dấu ngoặc nhọn
  • Ngắt dòng sau dấu ngoặc nhọn nếu dấu ngoặc nhọn đó kết thúc câu lệnh hoặc phần thân của hàm hoặc câu lệnh lớp hoặc phương thức lớp. Cụ thể, không có ngắt dòng sau dấu ngoặc nhọn nếu nó được theo sau bởi ________ 35 _______74, ________35 ______79, ________35 _______77 hoặc dấu phẩy, dấu chấm phẩy hoặc dấu ngoặc đơn bên phải

Thí dụ

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

4. 1. 3 khối trống. có thể ngắn gọn

Một khối trống hoặc cấu trúc giống như khối có thể được đóng lại ngay sau khi nó được mở ra, không có ký tự, dấu cách hoặc ngắt dòng ở giữa (i. e.

goog.module('search.urlHistory.UrlHistoryService');
81), trừ khi nó là một phần của câu lệnh nhiều khối (câu lệnh chứa trực tiếp nhiều khối.
goog.module('search.urlHistory.UrlHistoryService');
73/
goog.module('search.urlHistory.UrlHistoryService');
74 or
goog.module('search.urlHistory.UrlHistoryService');
84/
goog.module('search.urlHistory.UrlHistoryService');
79/
goog.module('search.urlHistory.UrlHistoryService');
86)

Thí dụ

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

không được phép

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

4. 2 Khối thụt đầu dòng. +2 dấu cách

Mỗi khi một khối mới hoặc cấu trúc giống như khối được mở, phần thụt lề sẽ tăng thêm hai khoảng trắng. When the block ends, the indent returns to the previous indent level. The indent level applies to both code and comments throughout the block. (Xem ví dụ trong)

4. 2. 1 mảng chữ. giống như khối tùy chọn

Any array literal may optionally be formatted as if it were a “block-like construct. ” Ví dụ: tất cả những điều sau đây đều hợp lệ (không phải là danh sách đầy đủ)

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

Các kết hợp khác được cho phép, đặc biệt khi nhấn mạnh các nhóm ngữ nghĩa giữa các phần tử, nhưng không nên chỉ được sử dụng để giảm kích thước dọc của các mảng lớn hơn

4. 2. 2 đối tượng chữ. giống như khối tùy chọn

Bất kỳ đối tượng theo nghĩa đen nào cũng có thể được định dạng tùy chọn như thể nó là một “cấu trúc giống như khối. ” Các ví dụ tương tự áp dụng như. Ví dụ: tất cả những điều sau đây đều hợp lệ (không phải là danh sách đầy đủ)

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 Lớp chữ

Các ký tự lớp (dù là khai báo hay biểu thức) được thụt vào dưới dạng khối. Không thêm dấu chấm phẩy sau các phương thức hoặc sau dấu ngoặc nhọn đóng của một khai báo lớp (các câu lệnh—chẳng hạn như các phép gán—có chứa các biểu thức lớp vẫn được kết thúc bằng dấu chấm phẩy). Sử dụng từ khóa

goog.module('search.urlHistory.UrlHistoryService');
87, nhưng không sử dụng chú thích
goog.module('search.urlHistory.UrlHistoryService');
88 JSDoc trừ khi lớp mở rộng một loại được tạo khuôn mẫu

Thí dụ

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

4. 2. 4 Biểu thức hàm

When declaring an anonymous function in the list of arguments for a function call, the body of the function is indented two spaces more than the preceding indentation depth

Thí dụ

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

4. 2. 5 Switch statements

Như với bất kỳ khối nào khác, nội dung của khối chuyển đổi được thụt vào +2

Sau nhãn chuyển đổi, một dòng mới xuất hiện và mức thụt đầu dòng được tăng lên +2, chính xác như thể một khối đang được mở. 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

Một dòng trống là tùy chọn giữa một

goog.module('search.urlHistory.UrlHistoryService');
89 và trường hợp sau

Thí dụ

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

4. 3 Tuyên bố

4. 3. 1 Một câu lệnh trên mỗi dòng

Mỗi câu lệnh được theo sau bởi một dấu ngắt dòng

4. 3. 2 dấu chấm phẩy là bắt buộc

Every statement must be terminated with a semicolon. Dựa vào chèn dấu chấm phẩy tự động bị cấm

4. 4 Giới hạn cột. 80

Mã JavaScript có giới hạn cột là 80 ký tự. Ngoại trừ như được lưu ý bên dưới, bất kỳ dòng nào vượt quá giới hạn này đều phải được ngắt dòng, như được giải thích trong

ngoại lệ

  1. Câu lệnh
    /* 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 và
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    75 (xem và )
  2. Câu lệnh mô-đun ES
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    68 và
    goog.module('search.urlHistory.UrlHistoryService');
    
    08 (xem và )
  3. Các dòng không thể tuân theo giới hạn cột hoặc sẽ cản trở khả năng khám phá. Những ví dụ bao gồm
    • Một URL dài có thể nhấp được trong nguồn
    • 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. , một đường dẫn tệp dài)

4. 5 Đóng gói dòng

Lưu ý thuật ngữ. Ngắt dòng đang chia một đoạn mã thành nhiều dòng để tuân theo giới hạn của cột, trong đó đoạn mã đó có thể nằm gọn trong một dòng một cách hợp pháp

There is no comprehensive, deterministic formula showing exactly how to line-wrap in every situation. Rất thường có một số cách hợp lệ để ngắt dòng cùng một đoạn mã

Ghi chú. Mặc dù lý do điển hình của việc ngắt dòng là để tránh vượt quá giới hạn cột, nhưng ngay cả mã thực tế phù hợp với giới hạn cột cũng có thể được ngắt dòng theo quyết định của tác giả

Mẹo. Trích xuất một phương thức hoặc biến cục bộ có thể giải quyết vấn đề mà không cần phải ngắt dòng

4. 5. 1 Where to break

Chỉ thị chính của line-wrapping là. prefer to break at a higher syntactic level

ưa thích

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

nản lòng

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. gán, chia, gọi hàm, tham số, hằng số

Toán tử được gói như sau

  1. When a line is broken at an operator the break comes after the symbol. (Lưu ý rằng đây không phải là cách làm tương tự được sử dụng theo phong cách Google dành cho Java. )
    1. Điều này không áp dụng cho dấu chấm (
      goog.module('search.urlHistory.UrlHistoryService');
      
      95), thực ra không phải là toán tử
  2. Tên phương thức hoặc hàm tạo được gắn vào dấu ngoặc đơn mở (
    goog.module('search.urlHistory.UrlHistoryService');
    
    96) theo sau nó
  3. Dấu phẩy (
    goog.module('search.urlHistory.UrlHistoryService');
    
    97) được gắn vào mã thông báo đứng trước nó

Ghi chú. Mục tiêu chính của việc ngắt dòng là có mã rõ ràng, không nhất thiết là mã phù hợp với số lượng dòng nhỏ nhất

4. 5. 2 dòng tiếp tục thụt lề ít nhất +4 dấu cách

Khi ngắt dòng, mỗi dòng sau dòng đầu tiên (mỗi dòng tiếp theo) được thụt vào ít nhất +4 so với dòng ban đầu, trừ khi nó tuân theo quy tắc thụt lề khối

Khi có nhiều dòng tiếp tục, thụt đầu dòng có thể thay đổi ngoài +4 khi thích hợp. Nói chung, các dòng tiếp tục ở cấp độ cú pháp sâu hơn được thụt vào bởi các bội số lớn hơn của 4 và hai dòng sử dụng cùng một mức độ thụt đầu dòng khi và chỉ khi chúng bắt đầu bằng các phần tử song song về mặt cú pháp

giải quyết thực tiễn không khuyến khích sử dụng một số khoảng trắng khác nhau để căn chỉnh các mã thông báo nhất định với các dòng trước đó

4. 6 Khoảng trắng

4. 6. 1 Khoảng trắng dọc

Một dòng trống duy nhất xuất hiện

  1. Giữa các phương thức liên tiếp trong một lớp hoặc đối tượng theo nghĩa đen
    1. Ngoại lệ. Một dòng trống giữa hai định nghĩa thuộc tính liên tiếp trong một đối tượng bằng chữ (không có mã nào khác giữa chúng) là tùy chọn. Các dòng trống như vậy được sử dụng khi cần thiết để tạo các nhóm trường hợp lý
  2. Trong nội dung phương thức, tiết kiệm để tạo các nhóm câu lệnh logic. Các dòng trống ở đầu hoặc cuối thân hàm không được phép
  3. Tùy chọn trước phương thức đầu tiên hoặc sau phương thức cuối cùng trong một lớp hoặc đối tượng theo nghĩa đen (không được khuyến khích cũng không được khuyến khích)
  4. Theo yêu cầu của các phần khác của tài liệu này (e. g. )

Nhiều dòng trống liên tiếp được cho phép, nhưng không bao giờ bắt buộc (cũng không được khuyến khích)

4. 6. 2 Khoảng trắng ngang

Việc sử dụng khoảng trắng ngang phụ thuộc vào vị trí và thuộc ba loại chính. đầu (ở đầu dòng), theo sau (ở cuối dòng) và nội bộ. Khoảng trắng hàng đầu (i. e. , indentation) is addressed elsewhere. Trailing whitespace is forbidden

Ngoài những nơi được yêu cầu bởi ngôn ngữ hoặc các quy tắc kiểu khác và ngoài chữ, nhận xét và JSDoc, một không gian ASCII bên trong cũng chỉ xuất hiện ở những vị trí sau

  1. Tách bất kỳ từ dành riêng nào (chẳng hạn như
    goog.module('search.urlHistory.UrlHistoryService');
    
    73,
    goog.module('search.urlHistory.UrlHistoryService');
    
    75 hoặc
    goog.module('search.urlHistory.UrlHistoryService');
    
    79) ngoại trừ
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    01 và
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    02, khỏi dấu ngoặc đơn mở (_______35_______96) theo sau nó trên dòng đó
  2. Tách bất kỳ từ dành riêng nào (chẳng hạn như
    goog.module('search.urlHistory.UrlHistoryService');
    
    74 hoặc
    goog.module('search.urlHistory.UrlHistoryService');
    
    79) khỏi dấu ngoặc nhọn đóng (
    goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    06) đứng trước từ đó trên dòng đó
  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. Trước một ký tự đối tượng là đối số đầu tiên của một hàm hoặc phần tử đầu tiên trong một ký tự mảng (e. g.
      goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
      goog.module('foo.bar.baz');
      
      08)
    2. Trong bản mở rộng mẫu, vì nó bị cấm bởi ngôn ngữ (e. g. có giá trị.
      goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
      goog.module('foo.bar.baz');
      
      09, không hợp lệ.
      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. It is not even required to maintain horizontal alignment in places where it was already used

Here is an example without alignment, followed by one with alignment. Both are allowed, but the latter is discouraged

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

Tip. Alignment can aid readability, but it creates problems for future maintenance. Consider a future change that needs to touch just one line. This change may leave the formerly-pleasing formatting mangled, and that is allowed. More often it prompts the coder (perhaps you) to adjust whitespace on nearby lines as well, possibly triggering a cascading series of reformattings. That one-line change now has a blast radius. This can at worst result in pointless busywork, but at best it still corrupts version history information, slows down reviewers and exacerbates merge conflicts

4. 6. 4 Function arguments

Prefer to put all function arguments on the same line as the function name. If doing so would exceed the 80-column limit, the arguments must be line-wrapped in a readable way. To save space, you may wrap as close to 80 as possible, or put each argument on its own line to enhance readability. Indentation should be four spaces. Aligning to the parenthesis is allowed, but discouraged. Below are the most common patterns for argument wrapping

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

Optional grouping parentheses are omitted only when the author and reviewer agree that there is no reasonable chance that the code will be misinterpreted without them, nor would they have made the code easier to read. It is not reasonable to assume that every reader has the entire operator precedence table memorized

Do not use unnecessary parentheses around the entire expression following

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');
24,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
25, or
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
26

Parentheses are required for type casts.

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

This section addresses implementation comments. JSDoc is addressed separately in

Block comments are indented at the same level as the surrounding code. They may be in

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
28 or
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
14-style. For multi-line
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
28 comments, subsequent lines must start with * aligned with the
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
31 on the previous line, to make comments obvious with no extra context

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

Comments are not enclosed in boxes drawn with asterisks or other characters

Do not use JSDoc (

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

“Parameter name” comments should be used whenever the value and method name do not sufficiently convey the meaning, and refactoring the method to be clearer is infeasible . Their preferred format is before the value with =

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

For consistency with surrounding code you may put them after the value without =

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 Language features

JavaScript includes many dubious (and even dangerous) features. This section delineates which features may or may not be used, and any additional constraints on their use

5. 1 Local variable declarations

5. 1. 1 Use
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33 and
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
34

Declare all local variables with either

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33 or
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
34. Use const by default, unless a variable needs to be reassigned. The
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
37 keyword must not be used

5. 1. 2 One variable per declaration

Every local variable declaration declares only one variable. declarations such as

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

5. 1. 3 Declared when needed, initialized as soon as possible

Local variables are not habitually declared at the start of their containing block or block-like construct. Instead, local variables are declared close to the point they are first used (within reason), to minimize their scope

5. 1. 4 Declare types as needed

JSDoc type annotations may be added either on the line above the declaration, or else inline before the variable name if no other JSDoc is present

Thí dụ

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

Mixing inline and JSDoc styles is not allowed. the compiler will only process the first JsDoc and the inline annotations will be lost

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. There are many cases where the compiler can infer a templatized type but not its parameters. This is particularly the case when the initializing literal or constructor call does not include any values of the template parameter type (e. g. , empty arrays, objects,

goog.module('search.urlHistory.UrlHistoryService');
64s, or
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
40s), or if the variable is modified in a closure. Local variable type annotations are particularly helpful in these cases since otherwise the compiler will infer the template parameter as unknown

5. 2 Array literals

5. 2. 1 Use trailing commas

Include a trailing comma whenever there is a line break between the final element and the closing bracket

Thí dụ

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 Do not use the variadic
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
41 constructor

The constructor is error-prone if arguments are added or removed. Use a literal instead

không được phép

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

Điều này hoạt động như mong đợi ngoại trừ trường hợp thứ ba. nếu

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
42 là một số nguyên thì
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
43 là một mảng có kích thước
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
42 trong đó tất cả các phần tử là
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
45. Nếu
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
42 là bất kỳ số nào khác, thì một ngoại lệ sẽ được đưa ra và nếu đó là bất kỳ số nào khác thì đó sẽ là một mảng một phần tử

Thay vào đó, hãy viết

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

Cho phép phân bổ rõ ràng một mảng có độ dài nhất định bằng cách sử dụng

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
47 khi thích hợp

5. 2. 3 Thuộc tính phi số

Không xác định hoặc sử dụng các thuộc tính không phải là số trên một mảng (ngoài

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
48). Thay vào đó, hãy sử dụng
goog.module('search.urlHistory.UrlHistoryService');
64 (hoặc
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
50)

5. 2. 4 Phá hủy

Các ký tự mảng có thể được sử dụng ở phía bên trái của phép gán để thực hiện hủy (chẳng hạn như khi giải nén nhiều giá trị từ một mảng đơn hoặc có thể lặp lại). Phần tử còn lại cuối cùng có thể được bao gồm (không có khoảng cách giữa

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51 và tên biến). Các yếu tố nên được bỏ qua nếu chúng không được sử dụng

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

Destructuring may also be used for function parameters (note that a parameter name is required but ignored). Luôn chỉ định

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
52 làm giá trị mặc định nếu tham số mảng bị hủy cấu trúc là tùy chọn và cung cấp các giá trị mặc định ở phía bên trái

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

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

không được phép

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

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

Mẹo. Để (không) đóng gói nhiều giá trị vào một tham số hoặc hàm trả về, hãy ưu tiên hủy đối tượng hơn là hủy mảng khi có thể, vì nó cho phép đặt tên các phần tử riêng lẻ và chỉ định một loại khác nhau cho từng phần tử

5. 2. 5 Toán tử trải rộng

Các ký tự mảng có thể bao gồm toán tử trải rộng (

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51) để làm phẳng các phần tử ra khỏi một hoặc nhiều lần lặp khác. Toán tử trải rộng nên được sử dụng thay vì các cấu trúc khó xử hơn với
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
54. Không có khoảng trống sau
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51

Thí dụ

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

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

5. 3 đối tượng chữ

5. 3. 1 Sử dụng dấu phẩy sau

Bao gồm dấu phẩy ở cuối bất cứ khi nào có dấu ngắt dòng giữa thuộc tính cuối cùng và dấu ngoặc nhọn đóng

5. 3. 2 Không sử dụng hàm tạo
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
50

Mặc dù

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
50 không gặp vấn đề giống như
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
41, nhưng nó vẫn không được phép vì tính nhất quán. Thay vào đó, hãy sử dụng một đối tượng theo nghĩa đen (
goog.module('search.urlHistory.UrlHistoryService');
81 hoặc
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
60)

5. 3. 3 Không trộn lẫn các phím được trích dẫn và không được trích dẫn

Các ký tự đối tượng có thể đại diện cho các cấu trúc (với các khóa và/hoặc ký hiệu không được trích dẫn) hoặc các ký tự (với các khóa được trích dẫn và/hoặc được tính toán). Không trộn lẫn các loại khóa này trong một đối tượng theo nghĩa đen

không được phép

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

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

Điều này cũng mở rộng để chuyển tên thuộc tính cho các hàm, như

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
61. Cụ thể, làm như vậy sẽ phá vỡ mã được biên dịch vì trình biên dịch không thể đổi tên/làm xáo trộn chuỗi ký tự

không được phép

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

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

Điều này được thực hiện tốt nhất như

/** @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

Thí dụ

/** @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

Thí dụ

/** @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

Thí dụ

/** @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)

Thí dụ

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

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

không được phép

/** @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

Thí dụ

/** @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. be careful of using any other built-in symbols (e. 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

không được phép

/** @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, default parameters, rest, and arrow functions should all be used when appropriate

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
93 allows for a class-like definition similar to ES6 class syntax

/** @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

Per-instance properties should be defined in the constructor after the call to the super class constructor, if there is a super class. Methods should be defined on the prototype of the constructor

Defining constructor prototype hierarchies correctly is harder than it first appears. For that reason, it is best to use

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
95 from the 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 . Mixins and modifying the prototypes of builtin objects are explicitly forbidden

Exception. Framework code (such as Polymer, or Angular) may need to use

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
81s, and should not resort to even-worse workarounds to avoid doing so

5. 4. 7 Getters and Setters

Do not use JavaScript getter and setter properties. They are potentially surprising and difficult to reason about, and have limited support in the compiler. Provide ordinary methods instead

Exception. there are situations where defining a getter or setter is unavoidable (e. g. data binding frameworks such as Angular and Polymer, or for compatibility with external APIs that cannot be adjusted). In these cases only, getters and setters may be used with caution, provided they are defined with the

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
00 and
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
01 shorthand method keywords or
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
02 (not
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
03, which interferes with property renaming). Getters must not change observable state

không được phép

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

5. 4. 8 Overriding toString

The

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
04 method may be overridden, but must always succeed and never have visible side effects

Tip. Beware, in particular, of calling other methods from toString, since exceptional conditions could lead to infinite loops

5. 4. 9 Interfaces

Interfaces may be declared with

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
05 or
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
06. Interfaces declared with
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
06 can be explicitly (i. e. via
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
08) or implicitly implemented by a class or object literal

All non-static method bodies on an interface must be empty blocks. Fields must be declared as uninitialized members in the class constructor

Thí dụ

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

5. 4. 10 Abstract Classes

Use abstract classes when appropriate. Abstract classes and methods must be annotated with

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
09. Không sử dụng
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
10. See abstract classes and methods

5. 5 Functions

5. 5. 1 Chức năng cấp cao nhất

Top-level functions 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 optionally exported. See for more on exports

ví dụ

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

5. 5. 2 Nested functions and closures

Các hàm có thể chứa các định nghĩa hàm lồng nhau. If it is useful to give the function a name, it should be assigned to a local

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

5. 5. 3 chức năng mũi tên

Hàm mũi tên cung cấp cú pháp hàm ngắn gọn và đơn giản hóa phạm vi

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 cho các hàm lồng nhau. Prefer arrow functions over the
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
01 keyword, particularly for nested functions (but see )

Thích các chức năng mũi tên hơn các phương pháp tiếp cận phạm vi

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 khác, chẳng hạn như
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
16,
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
17 và
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
18. Các hàm mũi tên đặc biệt hữu ích để gọi vào các cuộc gọi lại vì chúng cho phép chỉ định rõ ràng tham số nào sẽ chuyển đến cuộc gọi lại trong khi ràng buộc sẽ chuyển qua tất cả các tham số một cách mù quáng

Phía bên trái của mũi tên chứa 0 hoặc nhiều tham số. Dấu ngoặc đơn xung quanh các tham số là tùy chọn nếu chỉ có một tham số không bị hủy cấu trúc. Khi sử dụng dấu ngoặc đơn, các loại tham số nội tuyến có thể được chỉ định (xem phần )

Mẹo. Việc luôn sử dụng dấu ngoặc đơn ngay cả đối với các hàm mũi tên có một tham số có thể tránh được các trường hợp khi thêm tham số nhưng quên thêm dấu ngoặc đơn, có thể dẫn đến mã có thể phân tích cú pháp và không còn hoạt động như dự kiến

Phía bên phải của mũi tên chứa phần thân của hàm. Theo mặc định, phần thân là một câu lệnh khối (không hoặc nhiều câu lệnh được bao quanh bởi dấu ngoặc nhọn). Phần thân cũng có thể là một biểu thức đơn được trả về hoàn toàn nếu một trong hai. the program logic requires returning a value, or the

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
20 operator precedes a single function or method call (using
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
20 ensures
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
45 is returned, prevents leaking values, and communicates intent). Dạng biểu thức đơn được ưu tiên nếu nó cải thiện khả năng đọc (e. g. , cho các biểu thức ngắn hoặc đơn giản)

ví dụ

import './sideeffects.js';

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

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

không được phép

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 máy phát điện

Trình tạo cho phép một số trừu tượng hữu ích và có thể được sử dụng khi cần thiết

Khi xác định các hàm tạo, hãy đính kèm

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
31 vào từ khóa
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
01 khi có mặt và tách nó bằng khoảng trắng khỏi tên của hàm. Khi sử dụng lợi nhuận ủy quyền, hãy đính kèm
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
31 vào từ khóa
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
26

Thí dụ

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 Tham số và kiểu trả về

Function parameters and return types should usually be documented with JSDoc annotations. Xem để biết thêm thông tin

5. 5. 5. 1 Thông số mặc định

Các tham số tùy chọn được phép sử dụng toán tử bằng trong danh sách tham số. Các tham số tùy chọn phải bao gồm khoảng trắng ở cả hai bên của toán tử bằng, được đặt tên chính xác như các tham số bắt buộc (i. e. , không có tiền tố là

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
26), hãy sử dụng hậu tố
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
27 trong loại JSDoc của chúng, đặt sau các tham số bắt buộc và không sử dụng các trình khởi tạo tạo ra tác dụng phụ có thể quan sát được. Tất cả các tham số tùy chọn cho các chức năng cụ thể phải có giá trị mặc định, ngay cả khi giá trị đó là
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
45. In contrast to concrete functions, abstract and interface methods must omit default parameter values

Thí dụ

import './sideeffects.js';

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

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

Sử dụng các tham số mặc định một cách tiết kiệm. Ưu tiên phá hủy (như trong ) để tạo các API có thể đọc được khi có nhiều hơn một số ít tham số tùy chọn không có thứ tự tự nhiên

Ghi chú. Không giống như các tham số mặc định của Python, bạn có thể sử dụng các trình khởi tạo trả về các đối tượng có thể thay đổi mới (chẳng hạn như

goog.module('search.urlHistory.UrlHistoryService');
81 hoặc
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
52) vì trình khởi tạo được ước tính mỗi khi giá trị mặc định được sử dụng, do đó, một đối tượng sẽ không được chia sẻ giữa các lệnh gọi

Mẹo. Mặc dù các biểu thức tùy ý bao gồm các lệnh gọi hàm có thể được sử dụng làm trình khởi tạo, nhưng chúng nên được giữ càng đơn giản càng tốt. Tránh các trình khởi tạo hiển thị trạng thái có thể thay đổi được chia sẻ, vì điều đó có thể dễ dàng tạo ra sự ghép nối ngoài ý muốn giữa các lệnh gọi hàm

5. 5. 5. 2 Rest parameters

Sử dụng tham số còn lại thay vì truy cập

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
31. Các tham số còn lại được nhập bằng tiền tố
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51 trong JSDoc của chúng. Tham số còn lại phải là tham số cuối cùng trong danh sách. Không có khoảng cách giữa
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51 và tên tham số. Không đặt tên cho tham số còn lại
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
34. Never name a local variable or parameter
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
31, which confusingly shadows the built-in name

Thí dụ

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 thuốc gốc

Khai báo các hàm và phương thức chung khi cần thiết với

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
36 trong JSDoc phía trên định nghĩa hàm hoặc phương thức

5. 5. 7 Toán tử trải rộng

Function calls may use the spread operator (

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51). Ưu tiên toán tử trải rộng thành
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
38 khi một mảng hoặc khả năng lặp được giải nén thành nhiều tham số của hàm biến thiên. There is no space after the
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
51

Thí dụ

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 chuỗi ký tự

5. 6. 1 Sử dụng dấu nháy đơn

Ordinary string literals are delimited with single quotes (

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
40), rather than double quotes (
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
41)

Mẹo. nếu một chuỗi chứa một ký tự trích dẫn, hãy cân nhắc sử dụng chuỗi mẫu để tránh phải thoát khỏi trích dẫn

Ordinary string literals may not span multiple lines

5. 6. 2 Chữ mẫu

Sử dụng các ký tự mẫu (được phân tách bằng

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
42) trên cách nối chuỗi phức tạp, đặc biệt nếu có nhiều ký tự chuỗi liên quan. Mẫu chữ có thể kéo dài nhiều dòng

Nếu một mẫu chữ kéo dài nhiều dòng, thì nó không cần phải tuân theo sự thụt lề của khối kèm theo, mặc dù điều đó có thể xảy ra nếu khoảng trắng được thêm vào không thành vấn đề

Thí dụ

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 Không tiếp tục dòng

Không sử dụng các phần tiếp theo của dòng (nghĩa là kết thúc một dòng bên trong một chuỗi ký tự bằng dấu gạch chéo ngược) bằng ký tự chuỗi thông thường hoặc chuỗi mẫu. Mặc dù ES5 cho phép điều này, nhưng nó có thể dẫn đến các lỗi phức tạp nếu có bất kỳ khoảng trắng ở cuối nào xuất hiện sau dấu gạch chéo và ít rõ ràng hơn đối với người đọc

không được phép

import './sideeffects.js';

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

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

Thay vào đó, hãy viết

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 chữ số

Numbers may be specified in decimal, hex, octal, or binary. Sử dụng chính xác các tiền tố

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
43,
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
44 và
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
45, với các chữ cái viết thường, tương ứng cho hệ thập lục phân, bát phân và nhị phân. Không bao giờ thêm số 0 ở đầu trừ khi ngay sau nó là
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
46,
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
47 hoặc
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
48

5. 8 Cấu trúc điều khiển

5. 8. 1 Đối với các vòng lặp

Với ES6, ngôn ngữ hiện có ba loại vòng lặp

goog.module('search.urlHistory.UrlHistoryService');
75 khác nhau. Tất cả có thể được sử dụng, mặc dù các vòng lặp
goog.module('search.urlHistory.UrlHistoryService');
75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
25 nên được ưu tiên hơn khi có thể

Các vòng lặp

goog.module('search.urlHistory.UrlHistoryService');
75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
24 chỉ có thể được sử dụng trên các đối tượng kiểu dict (xem phần ) và không được sử dụng để lặp qua một mảng.
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
54 nên được sử dụng trong các vòng lặp
goog.module('search.urlHistory.UrlHistoryService');
75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
24 để loại trừ các thuộc tính nguyên mẫu không mong muốn. Ưu tiên ________ 35 _______75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
25 và
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
59 hơn ________35 _______75-
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
24 khi có thể

5. 8. 2 ngoại lệ

Exceptions are an important part of the language and should be used whenever exceptional cases occur. Luôn ném

goog.module('search.urlHistory.UrlHistoryService');
63s hoặc phân lớp của
goog.module('search.urlHistory.UrlHistoryService');
63. không bao giờ ném chuỗi ký tự hoặc các đối tượng khác. Luôn sử dụng
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
64 khi xây dựng một
goog.module('search.urlHistory.UrlHistoryService');
63

Cách xử lý này kéo dài đến các giá trị từ chối của

goog.module('search.urlHistory.UrlHistoryService');
65 vì
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
67 tương đương với
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
68 trong các hàm không đồng bộ

Các ngoại lệ tùy chỉnh cung cấp một cách tuyệt vời để truyền tải thông tin lỗi bổ sung từ các chức năng. They should be defined and used wherever the native

goog.module('search.urlHistory.UrlHistoryService');
63 type is insufficient

Prefer throwing exceptions over ad-hoc error-handling approaches (such as passing an error container reference type, or returning an object with an error property)

Rất hiếm khi đúng khi không làm gì để đáp lại một ngoại lệ bị bắt. When it truly is appropriate to take no action whatsoever in a catch block, the reason this is justified is explained in a comment

import './sideeffects.js';

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

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

không được phép

import '../directory/file';
0

Mẹo. Không giống như ở một số ngôn ngữ khác, các mẫu như trên đơn giản là không hoạt động vì điều này sẽ bắt lỗi do

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
70 đưa ra. Use
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
71 instead

5. 8. 3 Câu lệnh chuyển đổi

Terminology Note. Bên trong dấu ngoặc nhọn của khối chuyển đổi là một hoặc nhiều nhóm câu lệnh. Mỗi nhóm câu lệnh bao gồm một hoặc nhiều nhãn chuyển đổi (hoặc là

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
72 hoặc là
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
73), theo sau là một hoặc nhiều câu lệnh

5. 8. 3. 1 Fall-through. commented

Trong một khối chuyển đổi, mỗi nhóm câu lệnh hoặc kết thúc đột ngột (với ngoại lệ

goog.module('search.urlHistory.UrlHistoryService');
89,
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
21 hoặc
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
22n) hoặc được đánh dấu bằng một nhận xét để chỉ ra rằng việc thực thi sẽ hoặc có thể tiếp tục trong nhóm câu lệnh tiếp theo. Bất kỳ nhận xét nào truyền đạt ý tưởng về sự thất bại là đủ (thường là
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
77). Chú thích đặc biệt này không bắt buộc trong nhóm câu lệnh cuối cùng của khối chuyển đổi

Thí dụ

import '../directory/file';
1
5. 8. 3. 2 Trường hợp
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
78 có mặt

Mỗi câu lệnh chuyển đổi bao gồm một nhóm câu lệnh

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
78, ngay cả khi nó không chứa mã. The
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
78 statement group must be last

5. 9 cái này

Chỉ sử dụng

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 trong các hàm tạo và phương thức của lớp, trong các hàm mũi tên được định nghĩa trong các hàm tạo và phương thức của lớp hoặc trong các hàm có khai báo rõ ràng
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
82 trong JSDoc của hàm bao quanh ngay lập tức

Không bao giờ sử dụng

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 để chỉ đối tượng chung, bối cảnh của một
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
84, mục tiêu của một sự kiện hoặc các chức năng
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
85ed hoặc
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
86ed không cần thiết

5. 10 kiểm tra bình đẳng

Sử dụng các toán tử nhận dạng (

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
87/
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
88) ngoại trừ các trường hợp được nêu dưới đây

5. 10. 1 Exceptions Where Coercion is Desirable

Catching both

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

import '../directory/file';
2

5. 11 Disallowed features

5. 11. 1 với

Không sử dụng từ khóa

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
91. Nó làm cho mã của bạn khó hiểu hơn và đã bị cấm ở chế độ nghiêm ngặt kể từ ES5

5. 11. 2 Đánh giá mã động

Không sử dụng

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
84 hoặc hàm tạo
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
93 (ngoại trừ trình tải mã). Các tính năng này có khả năng gây nguy hiểm và đơn giản là không hoạt động trong môi trường CSP

5. 11. 3 Chèn dấu chấm phẩy tự động

Luôn kết thúc câu lệnh bằng dấu chấm phẩy (ngoại trừ khai báo hàm và lớp, như đã lưu ý ở trên)

5. 11. 4 Tính năng phi tiêu chuẩn

Không sử dụng các tính năng không chuẩn. Điều này bao gồm các tính năng cũ đã bị xóa (e. g. ,

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
94), các tính năng mới chưa được chuẩn hóa (e. g. , dự thảo làm việc TC39 hiện tại, các đề xuất ở bất kỳ giai đoạn nào hoặc các tiêu chuẩn web được đề xuất nhưng chưa hoàn chỉnh) hoặc các tính năng độc quyền chỉ được triển khai trong một số trình duyệt. Chỉ sử dụng các tính năng được xác định trong tiêu chuẩn ECMA-262 hoặc WHATWG hiện tại. (Lưu ý rằng các dự án viết dựa trên các API cụ thể, chẳng hạn như tiện ích mở rộng của Chrome hoặc Node. js, rõ ràng có thể sử dụng các API đó). Các "phần mở rộng" ngôn ngữ phi tiêu chuẩn (chẳng hạn như phần mở rộng được cung cấp bởi một số bộ chuyển mã bên ngoài) đều bị cấm

5. 11. 5 đối tượng Wrapper cho các kiểu nguyên thủy

Không bao giờ sử dụng

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
64 trên các trình bao bọc đối tượng nguyên thủy (
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), cũng như không bao giờ đưa chúng vào chú thích loại

không được phép

import '../directory/file';
3

Các trình bao bọc có thể được gọi là các hàm để ép buộc (được ưu tiên hơn là sử dụng

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 hoặc nối chuỗi trống) hoặc tạo ký hiệu

Thí dụ

import '../directory/file';
4

5. 11. 6 Sửa đổi các đối tượng dựng sẵn

Không bao giờ sửa đổi các kiểu dựng sẵn, bằng cách thêm các phương thức vào hàm tạo của chúng hoặc vào nguyên mẫu của chúng. Tránh phụ thuộc vào các thư viện làm điều này. Lưu ý rằng thư viện thời gian chạy của JSCompiler sẽ cung cấp các polyfill tuân thủ tiêu chuẩn nếu có thể;

Không thêm các ký hiệu vào đối tượng toàn cục trừ khi thực sự cần thiết (e. g. được yêu cầu bởi API của bên thứ ba)

5. 11. 7 Bỏ qua
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 khi gọi hàm tạo

Never invoke a constructor in a

goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
64 statement without using parentheses
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

không được phép

import '../directory/file';
5

sử dụng thay thế

import '../directory/file';
6

Bỏ qua dấu ngoặc đơn có thể dẫn đến những sai lầm tinh vi. Hai dòng này không tương đương

import '../directory/file';
7

6 Naming

6. 1 Quy tắc chung cho tất cả các định danh

Định danh chỉ sử dụng các chữ cái và chữ số ASCII, và trong một số ít trường hợp được lưu ý bên dưới, dấu gạch dưới và rất hiếm khi (khi được yêu cầu bởi các khung như Angular) ký hiệu đô la

Đặt tên mô tả càng tốt, trong lý do. Đừng lo lắng về việc tiết kiệm không gian theo chiều ngang vì điều quan trọng hơn nhiều là làm cho mã của bạn dễ hiểu ngay lập tức đối với người đọc mới. Không sử dụng các từ viết tắt mơ hồ hoặc không quen thuộc với người đọc bên ngoài dự án của bạn và không viết tắt bằng cách xóa các chữ cái trong một từ

import '../directory/file';
8

không được phép

import '../directory/file';
9

6. 2 Quy tắc theo loại định danh

6. 2. 1 tên gói

Tên gói đều là

goog.module('search.urlHistory.UrlHistoryService');
01. Ví dụ:
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, nhưng không phải là
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 hoặc
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 Tên lớp

Tên lớp, giao diện, bản ghi và typedef được viết bằng

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. Các lớp chưa được xuất chỉ đơn giản là các lớp cục bộ. chúng không được đánh dấu
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 và do đó không được đặt tên với dấu gạch dưới

Tên loại thường là danh từ hoặc cụm danh từ. Ví dụ:

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 hoặc
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. Ngoài ra, tên giao diện đôi khi có thể là tính từ hoặc cụm tính từ thay thế (ví dụ:
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 Tên phương thức

Tên phương thức được viết bằng

goog.module('search.urlHistory.UrlHistoryService');
01. Tên của phương thức
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 phải kết thúc bằng dấu gạch dưới

Tên phương thức thường là động từ hoặc cụm động từ. Ví dụ:

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 hoặc
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. Các phương thức getter và setter cho các thuộc tính không bao giờ được yêu cầu, nhưng nếu chúng được sử dụng thì chúng nên được đặt tên là
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 (hoặc tùy chọn là
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 hoặc
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 đối với booleans) hoặc
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 đối với setters

Dấu gạch dưới cũng có thể xuất hiện trong tên phương thức thử nghiệm JsUnit để phân tách các thành phần logic của tên. Một mẫu điển hình là

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, ví dụ như
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. Không có một cách chính xác nào để đặt tên cho các phương pháp thử nghiệm

6. 2. 4 tên liệt kê

Tên enum được viết bằng

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, tương tự như các lớp và thường phải là danh từ số ít. Các mục riêng lẻ trong enum được đặt tên trong
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 tên không đổi

Tên hằng sử dụng

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. tất cả các chữ cái viết hoa, với các từ được phân tách bằng dấu gạch dưới. Không có lý do gì để một hằng số được đặt tên với dấu gạch dưới ở cuối, vì các thuộc tính tĩnh riêng tư có thể được thay thế bằng các cục bộ mô-đun (ngầm riêng tư)

6. 2. 5. 1 Định nghĩa “hằng số”

Mỗi hằng số là một thuộc tính tĩnh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 hoặc một khai báo
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33 mô-đun-cục bộ, nhưng không phải tất cả các thuộc tính tĩnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 và các
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33 mô-đun cục bộ đều là hằng số. Trước khi chọn trường hợp không đổi, hãy xem xét liệu trường có thực sự giống như một hằng số bất biến sâu sắc hay không. Ví dụ: nếu bất kỳ trạng thái quan sát được nào của trường hợp đó có thể thay đổi, thì gần như chắc chắn đó không phải là hằng số. Chỉ có ý định không bao giờ thay đổi đối tượng nói chung là không đủ

ví dụ

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

Tên hằng thường là danh từ hoặc cụm danh từ

6. 2. 5. 2 Bí danh cục bộ

Các bí danh cục bộ nên được sử dụng bất cứ khi nào chúng cải thiện khả năng đọc so với các tên đủ điều kiện. Follow the same rules as

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74s (), maintaining the last part of the aliased name. Bí danh cũng có thể được sử dụng trong các chức năng. Bí danh phải là
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
33

ví dụ

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

6. 2. 6 Tên trường không cố định

Tên trường không cố định (tĩnh hoặc cách khác) được viết bằng

goog.module('search.urlHistory.UrlHistoryService');
01, với dấu gạch dưới cho các trường riêng tư

Những tên này thường là danh từ hoặc cụm danh từ. Ví dụ:

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 hoặc
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 Parameter names

Tên tham số được viết bằng

goog.module('search.urlHistory.UrlHistoryService');
01. Lưu ý rằng điều này áp dụng ngay cả khi tham số mong đợi một hàm tạo

Không nên sử dụng tên tham số một ký tự trong các phương thức công khai

Ngoại lệ. When required by a third-party framework, parameter names may begin with a

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. Ngoại lệ này không áp dụng cho bất kỳ số nhận dạng nào khác (e. g. biến cục bộ hoặc thuộc tính)

6. 2. 8 Tên biến cục bộ

Tên biến cục bộ được viết bằng

goog.module('search.urlHistory.UrlHistoryService');
01, ngoại trừ các hằng số mô-đun-cục bộ (cấp cao nhất), như được mô tả ở trên. Các hằng số trong phạm vi chức năng vẫn được đặt tên trong
goog.module('search.urlHistory.UrlHistoryService');
01. Lưu ý rằng
goog.module('search.urlHistory.UrlHistoryService');
01 được sử dụng ngay cả khi biến chứa hàm tạo

6. 2. 9 Tên tham số mẫu

Tên tham số mẫu phải ngắn gọn, mã định danh một từ hoặc một chữ cái và phải viết hoa toàn bộ, chẳng hạn như

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 hoặc
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 Tên mô-đun cục bộ

Tên mô-đun cục bộ không được xuất hoàn toàn là riêng tư. Chúng không được đánh dấu

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 và không kết thúc bằng dấu gạch dưới. Điều này áp dụng cho các lớp, hàm, biến, hằng, enum và các mã định danh mô-đun-cục bộ khác

6. 3 Vỏ lạc đà. xác định

Đôi khi, có nhiều cách hợp lý để chuyển đổi một cụm từ tiếng Anh thành trường hợp lạc đà, chẳng hạn như khi có từ viết tắt hoặc cấu trúc bất thường như IPv6 hoặc iOS. Để cải thiện khả năng dự đoán, Google Style chỉ định lược đồ xác định (gần như) sau đây

Bắt đầu với hình thức văn xuôi của tên

  1. Chuyển đổi cụm từ thành ASCII đơn giản và xóa mọi dấu nháy đơn. Ví dụ, thuật toán của Müller có thể trở thành thuật toán của Mueller
  2. Chia kết quả này thành các từ, phân tách trên khoảng trắng và bất kỳ dấu câu nào còn lại (thường là dấu gạch nối)
    1. Khuyến khích. if any word already has a conventional camel case appearance in common usage, split this into its constituent parts (e. g. , AdWords trở thành từ quảng cáo). Lưu ý rằng một từ chẳng hạn như iOS không thực sự ở dạng lạc đà;
  3. Bây giờ viết thường mọi thứ (bao gồm cả từ viết tắt), sau đó chỉ viết hoa ký tự đầu tiên của
    1. … mỗi từ, để tạo ra trường hợp lạc đà trên, hoặc
    2. … mỗi từ ngoại trừ từ đầu tiên, để mang lại trường hợp lạc đà thấp hơn
  4. Cuối cùng, nối tất cả các từ thành một định danh duy nhất

Lưu ý rằng cách viết hoa của các từ gốc gần như hoàn toàn bị bỏ qua

ví dụ

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

*Chấp nhận được, nhưng không khuyến khích

Ghi chú. Một số từ được gạch nối một cách mơ hồ trong ngôn ngữ tiếng Anh. ví dụ nonempty và non-empty đều đúng, vì vậy tên phương thức checkNonempty và checkNonEmpty cũng đúng

7 JSDoc

JSDoc được sử dụng trên tất cả các lớp, trường và phương thức

7. 1 Hình thức chung

Định dạng cơ bản của các khối JSDoc như trong ví dụ này

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

hoặc trong ví dụ một dòng này

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

Nếu nhận xét một dòng tràn thành nhiều dòng, thì nhận xét đó phải sử dụng kiểu nhiều dòng với

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 và
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 trên các dòng riêng của chúng

Nhiều công cụ trích xuất siêu dữ liệu từ các bình luận JSDoc để thực hiện xác thực và tối ưu hóa mã. Như vậy, những bình luận này phải được hình thành tốt

7. 2 đánh dấu

JSDoc được viết bằng Markdown, mặc dù nó có thể bao gồm HTML khi cần thiết

Lưu ý rằng các công cụ tự động giải nén JSDoc (e. g. JsDossier) thường sẽ bỏ qua định dạng văn bản thuần túy, vì vậy nếu bạn đã làm điều này

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

nó sẽ ra như thế này

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

Thay vào đó, hãy viết một danh sách Markdown

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

Kiểu Google cho phép một tập hợp con các thẻ JSDoc. Xem danh sách đầy đủ. Most tags must occupy their own line, with the tag at the beginning of the line

không được phép

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

Các thẻ đơn giản không yêu cầu bất kỳ dữ liệu bổ sung nào (chẳng hạn như

/* 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) có thể được kết hợp trên cùng một dòng, cùng với một loại tùy chọn khi thích hợp

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

Không có quy tắc cứng nào về thời điểm kết hợp các thẻ hoặc theo thứ tự nào, nhưng phải nhất quán

Để biết thông tin chung về các loại chú thích trong JavaScript, hãy xem Chú thích JavaScript cho Trình biên dịch đóng và Các loại trong Hệ thống loại đóng

7. Gói 4 dòng

Các thẻ khối được ngắt dòng được thụt vào bốn khoảng trắng. Văn bản mô tả được ngắt dòng có thể được xếp thẳng hàng với mô tả trên các dòng trước, nhưng việc căn chỉnh theo chiều ngang này không được khuyến khích

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

Không thụt lề khi ngắt dòng mô tả

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 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
70

Một tệp có thể có tổng quan về tệp cấp cao nhất. Thông báo bản quyền, thông tin tác giả và mặc định là tùy chọn. Tổng quan về tệp thường được khuyến nghị bất cứ khi nào một tệp bao gồm nhiều hơn một định nghĩa lớp. Nhận xét cấp cao nhất được thiết kế để hướng người đọc không quen thuộc với mã đến nội dung trong tệp này. Nếu có, nó có thể cung cấp mô tả về nội dung của tệp và mọi thông tin phụ thuộc hoặc tương thích. Các dòng được bọc không được thụt vào

Thí dụ

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

Các lớp, giao diện và bản ghi phải được ghi lại bằng mô tả và mọi tham số mẫu, giao diện được triển khai, khả năng hiển thị hoặc các thẻ thích hợp khác. Mô tả lớp phải cung cấp cho người đọc đủ thông tin để biết cách thức và thời điểm sử dụng lớp, cũng như bất kỳ cân nhắc bổ sung nào cần thiết để sử dụng đúng lớp. Mô tả văn bản có thể được bỏ qua trên hàm tạo. Chú thích

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 và
goog.module('search.urlHistory.UrlHistoryService');
88 không được sử dụng với từ khóa
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
97 trừ khi lớp được sử dụng để khai báo một
goog.module('my.test.helpers');
goog.module.declareLegacyNamespace();
goog.setTestOnly();
05 hoặc nó mở rộng một lớp chung

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

Tất cả các enum và typedef phải được ghi lại bằng các thẻ JSDoc thích hợp (

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 hoặc
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
73) ở dòng trước. Công khai enums và typedefs cũng phải có một mô tả. Các mục enum riêng lẻ có thể được ghi lại bằng nhận xét JSDoc trên dòng trước

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

Typedefs rất hữu ích để xác định các loại bản ghi ngắn hoặc bí danh cho các liên kết, các hàm phức tạp hoặc các loại chung. Typedefs nên tránh đối với các loại bản ghi có nhiều trường, vì chúng không cho phép ghi lại các trường riêng lẻ, cũng như không sử dụng mẫu hoặc tham chiếu đệ quy. Đối với các loại bản ghi lớn, hãy ưu tiên

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

Trong các phương thức và hàm được đặt tên, tham số và kiểu trả về phải được ghi lại, ngoại trừ trường hợp của các

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 có cùng chữ ký, trong đó tất cả các kiểu được bỏ qua. Loại
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
66 phải được ghi lại khi cần thiết. Kiểu trả về có thể được bỏ qua nếu hàm không có câu lệnh
goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
21 không trống

Các mô tả phương thức, tham số và trả về (nhưng không phải kiểu) có thể bị bỏ qua nếu chúng hiển nhiên từ phần còn lại của JSDoc của phương thức hoặc từ chữ ký của nó

Mô tả phương thức bắt đầu bằng một cụm động từ mô tả chức năng của phương thức. Cụm từ này không phải là một câu mệnh lệnh, mà thay vào đó được viết ở ngôi thứ ba, như thể có một ngụ ý. trước nó

Nếu một phương thức ghi đè một phương thức của lớp bậc trên, nó phải bao gồm một chú thích

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. Các phương thức bị ghi đè kế thừa tất cả các chú thích JSDoc từ phương thức siêu hạng (bao gồm cả các chú thích khả năng hiển thị) và chúng nên được bỏ qua trong phương thức bị ghi đè. Tuy nhiên, nếu bất kỳ loại nào được tinh chỉnh trong chú thích loại, thì tất cả chú thích
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 và
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 phải được chỉ định rõ ràng

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

Nếu bạn chỉ cần ghi lại thông số và kiểu trả về của hàm, bạn có thể tùy ý sử dụng JSDocs nội tuyến trong chữ ký của hàm. Các JSDoc nội tuyến này chỉ định các loại trả về và thông số không có thẻ

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

Nếu bạn cần mô tả hoặc thẻ, hãy sử dụng một nhận xét JSDoc duy nhất phía trên phương thức. Ví dụ: các phương thức trả về giá trị cần thẻ

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

Trong các chú thích chức năng ẩn danh thường là tùy chọn. Nếu suy luận loại tự động không đủ hoặc chú thích rõ ràng cải thiện khả năng đọc, thì hãy chú thích thông số và trả về các loại như thế này

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

Đối với các biểu thức loại chức năng, xem

Các loại tài sản phải được ghi lại. Mô tả có thể được bỏ qua đối với các thuộc tính riêng tư, nếu tên và loại cung cấp đủ tài liệu để hiểu mã

Các hằng số được xuất công khai được nhận xét giống như các thuộc tính

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

7. 10 Loại chú thích

Chú thích loại được tìm thấy trên các thẻ

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 và
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 và tùy chọn trên thẻ
/* 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 và bất kỳ thẻ hiển thị nào. Các chú thích loại được đính kèm với các thẻ JSDoc phải luôn được đặt trong dấu ngoặc nhọn

7. 10. 1 Nullability

Hệ thống loại định nghĩa các công cụ sửa đổi lần lượt là

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 và
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 cho non-null và nullable. Các công cụ sửa đổi này phải đứng trước loại

Công cụ sửa đổi tính vô hiệu có các yêu cầu khác nhau đối với các loại khác nhau, được chia thành hai loại chính

  1. Chú thích loại cho nguyên thủy (_______52_______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) và chữ (
    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 và
    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) luôn không thể vô hiệu theo mặc định. Sử dụng công cụ sửa đổi
    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 để biến nó thành null, nhưng bỏ qua phần dư thừa
    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
  2. Các loại tham chiếu (nói chung, mọi thứ trong
    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, bao gồm cả
    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) đề cập đến một lớp, enum, bản ghi hoặc typedef được xác định ở nơi khác. Vì các loại này có thể có hoặc không thể vô hiệu hóa, nên không thể chỉ từ cái tên mà biết nó có thể vô hiệu hóa hay không. Luôn sử dụng các công cụ sửa đổi rõ ràng
    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 và
    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 cho các loại này để tránh sự mơ hồ tại các trang web sử dụng

Xấu

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

Tốt

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

7. 10. 2 loại phôi

Trong trường hợp trình biên dịch không suy luận chính xác loại biểu thức và các hàm xác nhận trong goog. khẳng định không thể khắc phục nó, có thể thắt chặt loại bằng cách thêm nhận xét chú thích loại và đặt biểu thức trong ngoặc đơn. Lưu ý rằng dấu ngoặc đơn là bắt buộc

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

7. 10. 3 loại tham số mẫu

Luôn chỉ định tham số mẫu. Bằng cách này, trình biên dịch có thể thực hiện công việc tốt hơn và giúp người đọc dễ dàng hiểu mã làm gì hơn

Xấu

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

Tốt

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

Các trường hợp không nên sử dụng tham số mẫu

  • goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
    goog.module('foo.bar.baz');
    
    50 được sử dụng cho phân cấp kiểu và không phải là cấu trúc giống như bản đồ

7. 10. 4 Biểu thức kiểu hàm

Lưu ý thuật ngữ. biểu thức loại hàm đề cập đến một chú thích loại cho các loại hàm với từ khóa

goog.module('foo.bar');   // 'foo.bar.qux' would be fine, though
goog.module('foo.bar.baz');
01 trong chú thích (xem ví dụ bên dưới)

Khi định nghĩa hàm được đưa ra, không sử dụng biểu thức kiểu hàm. Chỉ định tham số và kiểu trả về với

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 và
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 hoặc với chú thích nội tuyến (xem phần ). Điều này bao gồm các hàm ẩn danh và các hàm được xác định và gán cho một const (trong đó hàm jsdoc xuất hiện phía trên toàn bộ biểu thức gán)

Cần có các biểu thức kiểu hàm, ví dụ, bên trong

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 hoặc
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. Cũng sử dụng nó cho các biến hoặc thuộc tính của kiểu hàm, nếu chúng không được khởi tạo ngay với định nghĩa hàm

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

Khi sử dụng biểu thức kiểu hàm, luôn chỉ định rõ ràng kiểu trả về. Mặt khác, loại trả về mặc định là không xác định (_______52_______73), dẫn đến hành vi kỳ lạ và không mong muốn, và hiếm khi là điều thực sự mong muốn

Xấu - gõ lỗi, nhưng không đưa ra cảnh báo

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

Tốt

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

7. 10. 5 khoảng trắng

Trong một chú thích loại, cần có một dấu cách hoặc ngắt dòng sau mỗi dấu phẩy hoặc dấu hai chấm. Các ngắt dòng bổ sung có thể được chèn vào để cải thiện khả năng đọc hoặc tránh vượt quá giới hạn cột. Những dấu ngắt này nên được chọn và thụt lề theo hướng dẫn hiện hành (e. g. và ). Không có khoảng trắng nào khác được phép trong chú thích loại

Tốt

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

Xấu

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

7. 11 chú thích hiển thị

Chú thích khả năng hiển thị (

/* 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) có thể được chỉ định trong khối
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
70 hoặc trên bất kỳ biểu tượng hoặc thuộc tính được xuất nào. Không chỉ định khả năng hiển thị cho các biến cục bộ, cho dù trong một chức năng hay ở cấp cao nhất của mô-đun. Tất cả tên
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 phải kết thúc bằng dấu gạch dưới

8 Policies

8. 1 Sự cố không được chỉ định bởi Google Style. Hãy nhất quán

Đối với bất kỳ câu hỏi về phong cách nào không được giải quyết dứt khoát bởi thông số kỹ thuật này, hãy ưu tiên thực hiện những gì mà mã khác trong cùng một tệp đang thực hiện. Nếu điều đó không giải quyết được câu hỏi, hãy xem xét mô phỏng các tệp khác trong cùng một gói

8. 2 Cảnh báo trình biên dịch

8. 2. 1 Sử dụng bộ cảnh báo tiêu chuẩn

Các dự án càng nhiều càng tốt nên sử dụng

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

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

8. 2. 2 Cách xử lý cảnh báo

Trước khi làm bất cứ điều gì, hãy chắc chắn rằng bạn hiểu chính xác những gì cảnh báo đang nói với bạn. Nếu bạn không chắc tại sao lại xuất hiện cảnh báo, hãy yêu cầu trợ giúp

Khi bạn hiểu cảnh báo, hãy thử các giải pháp sau theo thứ tự

  1. Đầu tiên, sửa chữa nó hoặc làm việc xung quanh nó. Thực hiện một nỗ lực mạnh mẽ để thực sự giải quyết cảnh báo hoặc tìm cách khác để hoàn thành nhiệm vụ tránh hoàn toàn tình huống
  2. Nếu không, hãy xác định xem đó có phải là báo động giả không. Nếu bạn tin rằng cảnh báo không hợp lệ và mã thực sự an toàn và chính xác, hãy thêm nhận xét để thuyết phục người đọc về sự thật này và áp dụng chú thích
    /** @const {number} */
    exports.CONSTANT_ONE = 1;
    
    /** @const {string} */
    exports.CONSTANT_TWO = 'Another constant';
    
    02
  3. Nếu không, hãy để lại nhận xét TODO. Đây là một phương sách cuối cùng. Nếu bạn làm điều này, đừng chặn cảnh báo. Cảnh báo sẽ hiển thị cho đến khi nó có thể được xử lý đúng cách

8. 2. 3 Loại bỏ cảnh báo ở phạm vi hợp lý hẹp nhất

Các cảnh báo bị chặn ở phạm vi hợp lý hẹp nhất, thường là của một biến cục bộ hoặc phương thức rất nhỏ. Thường thì một biến hoặc phương thức được trích xuất chỉ vì lý do đó

Thí dụ

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

Ngay cả một số lượng lớn các biện pháp ngăn chặn trong một lớp vẫn tốt hơn là làm mù cả lớp trước kiểu cảnh báo này

Đánh dấu các phương thức, lớp hoặc giao diện không dùng nữa bằng chú thích

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

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
03. Nhận xét không dùng nữa phải bao gồm hướng dẫn đơn giản, rõ ràng để mọi người khắc phục trang web cuộc gọi của họ

8. 4 Mã không có trong Google Style

Đôi khi, bạn sẽ gặp phải các tệp trong cơ sở mã của mình không đúng với Google Style. Những điều này có thể đến từ việc mua lại hoặc có thể đã được viết trước khi Google Style đưa ra quan điểm về một số vấn đề hoặc có thể không thuộc Google Style vì bất kỳ lý do nào khác

8. 4. 1 Định dạng lại mã hiện có

Khi cập nhật kiểu mã hiện có, hãy làm theo các hướng dẫn sau

  1. Không bắt buộc phải thay đổi tất cả mã hiện có để đáp ứng các nguyên tắc về kiểu dáng hiện tại. Định dạng lại mã hiện có là sự đánh đổi giữa mã rời và tính nhất quán. Các quy tắc về phong cách phát triển theo thời gian và những loại chỉnh sửa này để duy trì sự tuân thủ sẽ tạo ra sự xáo trộn không cần thiết. Tuy nhiên, nếu những thay đổi quan trọng đang được thực hiện đối với một tệp thì tệp đó sẽ nằm trong Google Style
  2. Hãy cẩn thận để không cho phép các sửa đổi phong cách cơ hội làm xáo trộn trọng tâm của CL. Nếu bạn thấy mình thực hiện nhiều thay đổi về phong cách không quan trọng đối với trọng tâm của CL, hãy quảng bá những thay đổi đó thành một CL riêng

8. 4. 2 Mã mới được thêm vào. sử dụng Google Phong cách

Các tệp hoàn toàn mới sử dụng Google Style, bất kể lựa chọn kiểu của các tệp khác trong cùng một gói

Khi thêm mã mới vào tệp không có trong Google Style, trước tiên nên định dạng lại mã hiện có, tuân theo lời khuyên trong

Nếu việc định dạng lại này không được thực hiện, thì mã mới phải nhất quán nhất có thể với mã hiện có trong cùng một tệp, nhưng không được vi phạm hướng dẫn về kiểu dáng

8. 5 Quy tắc phong cách cục bộ

Các nhóm và dự án có thể áp dụng các quy tắc kiểu bổ sung ngoài những quy tắc trong tài liệu này, nhưng phải chấp nhận rằng các thay đổi dọn dẹp có thể không tuân theo các quy tắc bổ sung này và không được chặn các thay đổi dọn dẹp đó do vi phạm bất kỳ quy tắc bổ sung nào. Cẩn thận với các quy tắc quá mức không phục vụ mục đích. Hướng dẫn về phong cách không tìm cách xác định phong cách trong mọi tình huống có thể xảy ra và bạn cũng vậy.

8. 6 Mã được tạo. chủ yếu được miễn

Mã nguồn được tạo bởi quá trình xây dựng không bắt buộc phải có trong Google Style. Tuy nhiên, mọi số nhận dạng được tạo sẽ được tham chiếu từ mã nguồn viết tay phải tuân theo các yêu cầu đặt tên. Là một ngoại lệ đặc biệt, các số nhận dạng như vậy được phép chứa dấu gạch dưới, điều này có thể giúp tránh xung đột với các số nhận dạng viết tay

9 phụ lục

9. 1 tham chiếu thẻ JSDoc

JSDoc phục vụ nhiều mục đích trong JavaScript. Ngoài việc được sử dụng để tạo tài liệu, nó còn được sử dụng để kiểm soát công cụ. Được biết đến nhiều nhất là các chú thích loại Trình biên dịch đóng cửa

9. 1. 1 Chú thích loại và các chú thích Trình biên dịch đóng cửa khác

Tài liệu cho JSDoc được Trình biên dịch đóng sử dụng được mô tả trong Chú thích JavaScript cho Trình biên dịch đóng và các loại trong Hệ thống loại đóng

9. 1. 2 Chú thích tài liệu

Ngoài JSDoc được mô tả trong Chú thích JavaScript cho Trình biên dịch đóng, các thẻ sau là phổ biến và được hỗ trợ tốt bởi các công cụ tạo tài liệu khác nhau (chẳng hạn như JsDossier) cho các mục đích tài liệu thuần túy

Bạn cũng có thể thấy các loại chú thích JSDoc khác trong mã của bên thứ ba. Các chú thích này xuất hiện trong Tham chiếu thẻ của Bộ công cụ JSDoc nhưng không được coi là một phần của kiểu Google hợp lệ

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

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

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
05 - Không nên dùng

Không được khuyến khích

cú pháp.

/** @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

Tài liệu về tác giả của một tệp hoặc chủ sở hữu của một bài kiểm tra, thường chỉ được sử dụng trong nhận xét

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

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
05 được sử dụng bởi bảng điều khiển kiểm tra đơn vị để xác định ai sở hữu kết quả kiểm tra

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

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

cú pháp.

/** @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

Cho biết những lỗi mà chức năng kiểm tra đã cho kiểm tra hồi quy

Mỗi lỗi nên có dòng

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

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
09 riêng, để giúp việc tìm kiếm các bài kiểm tra hồi quy trở nên dễ dàng nhất có thể

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

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
12 - Không dùng nữa. Không được dùng

không dùng nữa. Không được dùng. Sử dụng backticks Markdown để thay thế

cú pháp.

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

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

Trong lịch sử,

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

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
14 được viết là
/** @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

Historical note. @link tags have also been used to create external links in generated documentation. For external links, always use Markdown's link syntax instead

/* 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

Syntax.

/** @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

Reference a lookup to another class function or method

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

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

Syntax.

/** @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

Used in a fileoverview to indicate what browsers are supported by the 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. Authors are allowed to change its output, and reviewers are allowed to ask for such changes; disputes are worked out in the usual way. 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. Hai danh sách phải được phân tách bằng một dòng trống

Tương tự như câu lệnh nhập trong các ngôn ngữ khác, câu lệnh

goog.module('search.urlHistory.UrlHistoryService');
18 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 phải được viết trên một dòng, ngay cả khi chúng vượt quá giới hạn độ dài dòng 80 cột

Các dòng nên được sắp xếp theo thứ tự bảng chữ cái, với các chữ cái viết hoa đến trước

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

Tất cả các thành viên được xác định trên một lớp phải ở trong cùng một tệp. Chỉ nên cung cấp các lớp cấp cao nhất trong một tệp chứa nhiều thành viên được định nghĩa trên cùng một lớp (e. g. enums, các lớp bên trong, v.v.)

Làm cái này

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

Không phải cái này

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

Thành viên trên không gian tên cũng có thể được cung cấp

/* 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

WARNING.

/** @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

Only one

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

/** @const {string} */
exports.CONSTANT_TWO = 'Another constant';
64 invocation may be added 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

Thích sử dụng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 thay vì
goog.module('search.urlHistory.UrlHistoryService');
25 để phá vỡ sự phụ thuộc vòng tròn giữa các tệp trong cùng một thư viện. 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