Hàm tạo lớp javascript

Tài liệu này đóng vai trò là định nghĩa đầy đủ về các tiêu chuẩn viết mã của Google dành cho mã nguồn bằng ngôn ngữ lập trình JavaScript. Tệp nguồn JavaScript được mô tả là ở trong Google Style khi và chỉ khi nó tuân thủ các quy tắc ở đây

Giống như các hướng dẫn về phong cách lập trình khác, các vấn đề được đề cập không chỉ là các vấn đề thẩm mỹ về định dạng mà còn các loại quy ước hoặc tiêu chuẩn mã hóa khác. Tuy nhiên, tài liệu này tập trung chủ yếu vào các quy tắc cứng rắn và nhanh chóng mà chúng tôi tuân theo trên toàn cầu và tránh đưa ra lời khuyên không thể thực thi rõ ràng [dù là do con người hay công cụ]

1. 1 Ghi chú thuật ngữ

Trong tài liệu này, trừ khi được giải thích khác

  1. Thuật ngữ nhận xét luôn đề cập đến nhận xét triển khai. Chúng tôi không sử dụng cụm từ nhận xét tài liệu, thay vào đó sử dụng thuật ngữ chung “JSDoc” cho cả chú thích văn bản mà con người có thể đọc được và chú thích mà máy có thể đọc được trong

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

  2. Hướng dẫn Phong cách này sử dụng thuật ngữ RFC 2119 khi sử dụng các cụm từ phải, không được, nên, không nên và có thể. Các thuật ngữ thích và tránh tương ứng với nên và không nên tương ứng. Các câu mệnh lệnh và tuyên bố là quy định và tương ứng với phải

Các ghi chú thuật ngữ khác sẽ thỉnh thoảng xuất hiện trong toàn bộ tài liệu

1. 2 Ghi chú hướng dẫn

Mã ví dụ trong tài liệu này là phi quy chuẩn. Nghĩa là, trong khi các ví dụ ở trong Google Style, chúng có thể không minh họa cách duy nhất để thể hiện mã. Các lựa chọn định dạng tùy chọn được thực hiện trong các ví dụ không được thực thi như các quy tắc

2 Thông tin cơ bản về tệp nguồn

2. 1 Tên tệp

Tên tệp phải là chữ thường và có thể bao gồm dấu gạch dưới [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
49] hoặc dấu gạch ngang [
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
50], nhưng không có dấu chấm câu bổ sung. Thực hiện theo quy ước mà dự án của bạn sử dụng. Phần mở rộng của tên tệp phải là
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
51

2. 2 Mã hóa tập tin. UTF-8

Các tệp nguồn được mã hóa bằng 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ó một chuỗi thoát đặc biệt [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
52,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
53,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
54,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
55,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
56,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
57,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
58,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
59,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
60], chuỗi đó được sử dụng thay vì chuỗi 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

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

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. Nếu điều đó xảy ra, các chương trình đó bị hỏng và chúng phải được sửa

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à
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
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. Các câu lệnh ES
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    68, 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ó

Nhìn thấy ??

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

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à 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

Ví dụ

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

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

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

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 đơn

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

Ví dụ

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

/* 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, nên 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 đố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à 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 cục bộ mô-đun 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ụ

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

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ố

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
1
/* 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';
13. 4. 1. 3 Đặt tên nhập khẩu3. 4. 1. 3. 1 Nhập mô-đun đặt tên

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
500] là tên
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501 đượ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 bắt nguồn 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ì xuất mặc định bị cấm theo hướng dẫn kiểu này, hãy 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 [______3502] nên giữ nguyên tên. Tránh nhập bí danh [______3503]. Thích sửa các xung đột tên bằng cách sử dụng nhập mô-đun [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
504] hoặc tự đổi tên các 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 hoặc đường dẫn tệp 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 cục bộ mô-đun 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
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
507

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

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
52
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
533. 4. 2. 4 xuất khẩu từ

Các câu lệnh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
508 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ả hai hương vị
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
508

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

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
512, nhập Closure's
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
513

/* 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';
513 chỉ xuất một tập hợp con các thuộc tính từ
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
512 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. , ký hiệu được tạo bởi
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66] và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 sẽ trả về giá trị

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
593. 4. 4. 3 Khai báo ID mô-đun đóng trong mô-đun ES

Có thể sử dụng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
521 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,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
524d,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
525'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
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
524] cho ID mô-đun từ
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
521 sẽ luôn trả về đối tượng mô-đun [như thể nó là
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
504'd]. Do đó, đối số của _____3521 phải luôn kết thúc bằng _____3533

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ừ các 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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
521 để nâng cấp tệp Đóng lên mô-đun ES tại chỗ, nơi sử dụng xuất có tên

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

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

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

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 lệnh gọi tới
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
540

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 một 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à trong các 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 các 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 tuân theo khai báo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 được phân tách bằng một dòng trống. 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â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 không đổi duy nhất hoặc nếu không thì được cấu trúc thành một số bí danh không đổi. 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 đủ điều kiện ở 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ư

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
561,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
562,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
563,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
564 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
565 [để 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ó, cuộc gọi phải là một

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 [không phải một
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
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

Ví dụ

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

nản lòng

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

không được phép

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

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. Lưu ý rằng, bởi ??

Mẹo. Sử dụng

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
573,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
574,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
575,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
576,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
577, cũng như bất kỳ cái 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

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

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

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

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ó dấu ngắt dòng sau dấu ngoặc nhọn nếu theo sau nó là
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    574,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    579,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    577 hoặc dấu phẩy, dấu chấm phẩy hoặc dấu ngoặc đơn bên phải

Ví dụ

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

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.

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
581], 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.
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
573/
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
574 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
584/
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
579/
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
586]

Ví dụ

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

không được phép

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

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. Khi khối kết thúc, thụt lề trở về mức thụt lề trước đó. Mức thụt lề áp dụng cho cả mã và nhận xét trong toàn bộ khối. [Xem ví dụ trong ??]

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

Bất kỳ mảng chữ 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. ” Ví dụ: tất cả những điều sau đây đều hợp lệ [không phải là danh sách đầy đủ]

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

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ự được á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 đủ]

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
587, nhưng không phải chú thích
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
588 JSDoc trừ khi lớp mở rộng một loại được tạo khuôn mẫu

Ví dụ

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

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

Khi khai báo một hàm ẩn danh trong danh sách các đối số cho một lệnh gọi hàm, phần thân của hàm được thụt lề nhiều hơn hai khoảng trắng so với độ sâu thụt lề trước đó

Ví dụ

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

4. 2. 5 Câu lệnh chuyển đổi

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

After a switch label, a newline appears, and the indentation level is increased +2, exactly as if a block were being opened. Một khối rõ ràng có thể được sử dụng nếu được yêu cầu bởi phạm vi từ vựng. Nhãn công tắc sau trở về mức thụt đầu dòng trước đó, như thể một khối đã bị đóng

A blank line is optional between a

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

Ví dụ

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

4. 3 Tuyên bố

4. 3. 1 One statement per line

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

Mọi câu lệnh phải được kết thúc bằng dấu chấm phẩy. Relying on automatic semicolon insertion is forbidden

4. 4 Column limit. 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. ES module
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    68 and
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    508 statements [see ?? and ??]
  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
    • Một lệnh trình bao dự định được sao chép và dán
    • Một chuỗi ký tự dài có thể cần được sao chép hoặc tìm kiếm toàn bộ [e. g. , một đường dẫn tệp dài]

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

Lưu ý thuật ngữ. Line wrapping is breaking a chunk of code into multiple lines to obey column limit, where the chunk could otherwise legally fit in a single line

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

Note. 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 Nơi để phá vỡ

Chỉ thị chính của line-wrapping là. thích phá vỡ ở cấp độ cú pháp cao hơn

Preferred

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

nản lòng

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

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ố

Operators are wrapped as follows

  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 [
      /* Poor: the reader has no idea what character this is. */
      const units = '\u03bcs';
      
      595], thực tế không phải là toán tử
  2. A method or constructor name stays attached to the open parenthesis [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    596] that follows it
  3. A comma [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    597] stays attached to the token that precedes it

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

4. 5. 2 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

When there are multiple continuation lines, indentation may be varied beyond +4 as appropriate. 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

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

4. 6 Khoảng trắng

4. 6. 1 Vertical whitespace

A single blank line appears

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

Multiple consecutive blank lines are permitted, but never required [nor encouraged]

4. 6. 2 Horizontal whitespace

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

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

  1. Separating any reserved word [such as
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    573,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    575, or
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    579] except for
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    601 and
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    602, from an open parenthesis [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    596] that follows it on that line
  2. Separating any reserved word [such as
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    574 or
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    579] from a closing curly brace [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    606] that precedes it on that line
  3. Before any open curly brace [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    607], with two exceptions
    1. Before an object literal that is the first argument of a function or the first element in an array literal [e. g.
      /* Poor: the reader has no idea what character this is. */
      const units = '\u03bcs';
      
      608]
    2. In a template expansion, as it is forbidden by the language [e. g. valid.
      /* Poor: the reader has no idea what character this is. */
      const units = '\u03bcs';
      
      609, invalid.
      /* Poor: the reader has no idea what character this is. */
      const units = '\u03bcs';
      
      610]
  4. On both sides of any binary or ternary operator
  5. After a comma [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    597] or semicolon [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    612]. Note that spaces are never allowed before these characters
  6. After the colon [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    613] in an object literal
  7. On both sides of the double slash [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    614] 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.
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    615; or
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    616; or
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    617]

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

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

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

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

4. 7 Grouping parentheses. recommended

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. Không hợp lý khi cho rằng mọi đầu đọc đều ghi nhớ toàn bộ bảng ưu tiên toán tử

Không sử dụng dấu ngoặc đơn không cần thiết xung quanh toàn bộ biểu thức sau

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
618,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
619,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
620,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
621,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
622,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
623,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
624,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
625 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
626

Parentheses are required for type casts.

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

Phần này giải quyết các ý kiến ​​​​thực hiện. JSDoc được xử lý riêng trong ??

Block comments are indented at the same level as the surrounding code. Chúng có thể ở kiểu

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
628 hoặc kiểu
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
614. For multi-line
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
628 comments, subsequent lines must start with * aligned with the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
631 on the previous line, to make comments obvious with no extra context

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

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

Không sử dụng JSDoc [______048] để nhận xét triển khai

“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 . Định dạng ưa thích của họ là trước giá trị với =

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

Để thống nhất với mã xung quanh, bạn có thể đặt chúng sau giá trị mà không có =

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

5 Tính năng ngôn ngữ

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 Khai báo biến cục bộ

5. 1. 1 Sử dụng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
634

Khai báo tất cả các biến cục bộ bằng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
634. Sử dụng const theo mặc định, trừ khi một biến cần được gán lại. The
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 keyword must not be used

5. 1. 2 Mỗi khai báo một biến

Every local variable declaration declares only one variable. khai báo như

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
638 không được sử dụng

5. 1. 3 Khai báo khi cần thiết, khởi tạo ngay khi có thể

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

Các chú thích kiểu JSDoc có thể được thêm vào dòng phía trên khai báo hoặc nội tuyến khác trước tên biến nếu không có JSDoc nào khác xuất hiện

Ví dụ

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

Không được phép trộn các kiểu nội tuyến và JSDoc. the compiler will only process the first JsDoc and the inline annotations will be lost

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

Mẹo. Có nhiều trường hợp trình biên dịch có thể suy ra một kiểu đã được tạo khuôn mẫu nhưng không thể suy ra các tham số của nó. This is particularly the case when the initializing literal or constructor call does not include any values of the template parameter type [e. g. , mảng trống, đối tượng,

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
564 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
640] hoặc nếu biến được sửa đổi trong một bao đóng. Chú thích loại biến cục bộ đặc biệt hữu ích trong những trường hợp này vì nếu không, trình biên dịch sẽ suy ra tham số mẫu là không xác định

5. 2 mảng chữ

5. 2. 1 Use trailing commas

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

Ví dụ

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

5. 2. 2 Không sử dụng hàm tạo biến đổi
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
641

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

không được phép

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
642 is a whole number then
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
643 is an array of size
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
642 where all elements are
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
645. If
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
642 is any other number, then an exception will be thrown, and if it is anything else then it will be a single-element array

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

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

Explicitly allocating an array of a given length using

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

5. 2. 3 Non-numeric properties

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
648]. Use a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
564 [or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
650] instead

5. 2. 4 Destructuring

Array literals may be used on the left-hand side of an assignment to perform destructuring [such as when unpacking multiple values from a single array or iterable]. 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

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

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

Destructuring may also be used for function parameters [note that a parameter name is required but ignored]. Always specify

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
652 as the default value if a destructured array parameter is optional, and provide default values on the left hand side

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

không được phép

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

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 Spread operator

Array literals may include the spread operator [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
651] to flatten elements out of one or more other iterables. The spread operator should be used instead of more awkward constructs with
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
654. There is no space after the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
651

Ví dụ

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

5. 3 Object literals

5. 3. 1 Use trailing commas

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

5. 3. 2 Do not use the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
650 constructor

While

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
650 does not have the same problems as
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
641, it is still disallowed for consistency. Use an object literal [
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
581 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
660] instead

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

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

không được phép

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
661. In particular, doing so will break in compiled code because the compiler cannot rename/obfuscate the string literal

không được phép

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

This is best implemented as

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

5. 3. 4 Computed property names

Computed property names [e. g. ,

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
662] 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. ,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
663]. 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 [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
664] in place of a colon immediately followed by a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
601 or arrow function literal

Ví dụ

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

Lưu ý rằng ________ 3666 trong một phương thức tốc ký hoặc

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
601 đề cập đến chính đối tượng theo nghĩa đen trong khi ________ 3666 trong một hàm mũi tên đề cập đến phạm vi bên ngoài đối tượng theo nghĩa đen

Ví dụ

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

5. 3. 6 Shorthand properties

Shorthand properties are allowed on object literals

Ví dụ

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

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 [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
669, rather than
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
670], and if a destructured object is itself optional, it must default to
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
581. The JSDoc for the destructured parameter may be given any name [the name is unused but is required by the compiler]

Ví dụ

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

không được phép

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

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

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

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

5. 4 Classes

5. 4. 1 Constructors

Constructors are optional. Subclass constructors must call

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
674 before setting any fields or otherwise accessing
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666. 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,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
678,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
679], 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'
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
681

Ví dụ

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
645 in the constructor to prevent later shape changes. Adding
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
683 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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
663 method should be defined for any classes that are logically iterable. Beyond this,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
685 should be used sparingly

Tip. be careful of using any other built-in symbols [e. g. ,

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

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

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

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. Các khung yêu cầu giá trị

    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    666 đã biết trước khi gọi hàm tạo của siêu lớp, vì các hàm tạo với siêu lớp ES6 không có quyền truy cập vào giá trị
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    666 của thể hiện cho đến khi lệnh gọi tới
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    602 trả về

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
634,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633, default parameters, rest, and arrow functions should all be used when appropriate

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
693 allows for a class-like definition similar to ES6 class syntax

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

Alternatively, while

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
693 should be preferred for all new code, more traditional syntax is also allowed

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

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

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

5. 4. 6 Do not manipulate
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
681s directly

The

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
697 keyword allows clearer and more readable class definitions than defining
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
681 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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
100 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
101 shorthand method keywords or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
102 [not
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
103, which interferes with property renaming]. Getters must not change observable state

không được phép

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

5. 4. 8 Overriding toString

The

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
105 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
106. Interfaces declared with
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
106 can be explicitly [i. e. via
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
108] 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

Ví dụ

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

5. 4. 10 Abstract Classes

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
109. Do not use
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
110. See abstract classes and methods

5. 5 Functions

5. 5. 1 Top-level functions

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ụ

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

5. 5. 2 Hàm lồng nhau và bao đóng

Functions may contain nested function definitions. If it is useful to give the function a name, it should be assigned to a local

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

5. 5. 3 Arrow functions

Arrow functions provide a concise function syntax and simplify scoping

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 for nested functions. Prefer arrow functions over the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
601 keyword, particularly for nested functions [but see ??]

Prefer arrow functions over other

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 scoping approaches such as
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
116,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
117, and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
118. Arrow functions are particularly useful for calling into callbacks as they permit explicitly specifying which parameters to pass to the callback whereas binding will blindly pass along all parameters

The left-hand side of the arrow contains zero or more parameters. Parentheses around the parameters are optional if there is only a single non-destructured parameter. When parentheses are used, inline parameter types may be specified [see ??]

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

The right-hand side of the arrow contains the body of the function. By default the body is a block statement [zero or more statements surrounded by curly braces]. The body may also be an implicitly returned single expression if either. the program logic requires returning a value, or the

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
620 operator precedes a single function or method call [using
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
620 ensures
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
645 is returned, prevents leaking values, and communicates intent]. The single expression form is preferred if it improves readability [e. g. , for short or simple expressions]

ví dụ

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

không được phép

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

5. 5. 4 Generators

Generators enable a number of useful abstractions and may be used as needed

When defining generator functions, attach the

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
631 to the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
601 keyword when present, and separate it with a space from the name of the function. When using delegating yields, attach the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
631 to the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
626 keyword

Ví dụ

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

5. 5. 5 Parameter and return types

Function parameters and return types should usually be documented with JSDoc annotations. See ?? for more information

5. 5. 5. 1 Default parameters

Optional parameters are permitted using the equals operator in the parameter list. Optional parameters must include spaces on both sides of the equals operator, be named exactly like required parameters [i. e. , not prefixed with

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
126], use the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
127 suffix in their JSDoc type, come after required parameters, and not use initializers that produce observable side effects. All optional parameters for concrete functions must have default values, even if that value is
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
645. In contrast to concrete functions, abstract and interface methods must omit default parameter values

Ví dụ

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

Use default parameters sparingly. Prefer destructuring [as in ??] to create readable APIs when there are more than a small handful of optional parameters that do not have a natural order

Note. Unlike Python's default parameters, it is okay to use initializers that return new mutable objects [such as

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
581 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
652] because the initializer is evaluated each time the default value is used, so a single object won't be shared across invocations

Tip. While arbitrary expressions including function calls may be used as initializers, these should be kept as simple as possible. Avoid initializers that expose shared mutable state, as that can easily introduce unintended coupling between function calls

5. 5. 5. 2 Thông số nghỉ ngơi

Use a rest parameter instead of accessing

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
131. Rest parameters are typed with a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
651 prefix in their JSDoc. The rest parameter must be the last parameter in the list. There is no space between the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
651 and the parameter name. Do not name the rest parameter
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
134. Never name a local variable or parameter
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
131, which confusingly shadows the built-in name

Ví dụ

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

5. 5. 6 Generics

Declare generic functions and methods when necessary with

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
136 in the JSDoc above the function or method definition

5. 5. 7 Spread operator

Function calls may use the spread operator [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
651]. Prefer the spread operator to
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
138 when an array or iterable is unpacked into multiple parameters of a variadic function. There is no space after the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
651

Ví dụ

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

5. 6 String literals

5. 6. 1 Use single quotes

Ordinary string literals are delimited with single quotes [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
140], rather than double quotes [
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
141]

Tip. if a string contains a single quote character, consider using a template string to avoid having to escape the quote

Ordinary string literals may not span multiple lines

5. 6. 2 Template literals

Use template literals [delimited with

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
142] over complex string concatenation, particularly if multiple string literals are involved. Template literals may span multiple lines

If a template literal spans multiple lines, it does not need to follow the indentation of the enclosing block, though it may if the added whitespace does not matter

Ví dụ

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

5. 6. 3 No line continuations

Do not use line continuations [that is, ending a line inside a string literal with a backslash] in either ordinary or template string literals. Even though ES5 allows this, it can lead to tricky errors if any trailing whitespace comes after the slash, and is less obvious to readers

không được phép

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

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

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

5. 7 Number literals

Numbers may be specified in decimal, hex, octal, or binary. Use exactly

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
143,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
144, and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
145 prefixes, with lowercase letters, for hex, octal, and binary, respectively. Never include a leading zero unless it is immediately followed by
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
146,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
147, or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
148

5. 8 Control structures

5. 8. 1 For loops

With ES6, the language now has three different kinds of

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
575 loops. All may be used, though
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
575-
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
625 loops should be preferred when possible

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
575-
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
624 loops may only be used on dict-style objects [see ??], and should not be used to iterate over an array.
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
154 should be used in
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
575-
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
624 loops to exclude unwanted prototype properties. Prefer
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
575-
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
625 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
159 over
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
575-
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
624 when possible

5. 8. 2 Exceptions

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
563s or subclasses of
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
563. never throw string literals or other objects. Always use
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
164 when constructing an
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
563

This treatment extends to

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
565 rejection values as
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
167 is equivalent to
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
168 in async functions

Custom exceptions provide a great way to convey additional error information from functions. Chúng nên được xác định và sử dụng bất cứ nơi nào loại

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
563 gốc không đủ

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]

It is very rarely correct to do nothing in response to a caught exception. Khi thực sự thích hợp để không thực hiện bất kỳ hành động nào trong một khối bắt, lý do điều này là hợp lý được giải thích trong một nhận xét

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

không được phép

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

Tip. 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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
170 đưa ra. Sử dụng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
171 để thay thế

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

Lưu ý thuật ngữ. 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à

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
172 hoặc là
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
173], theo sau là một hoặc nhiều câu lệnh

5. 8. 3. 1 mùa thu. nhận xét

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ệ

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
589,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
621 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
622n] 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à
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
177]. 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

Ví dụ

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
115. 8. 3. 2 Trường hợp
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
178 có mặt

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
178, ngay cả khi nó không chứa mã. Nhóm tuyên bố
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
178 phải là cuối cùng

5. 9 this

Only use

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 in class constructors and methods, in arrow functions defined within class constructors and methods, or in functions that have an explicit
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
182 declared in the immediately-enclosing function’s JSDoc

Never use

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 to refer to the global object, the context of an
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
184, the target of an event, or unnecessarily
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
185ed or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
186ed functions

5. 10 Equality Checks

Use identity operators [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
187/
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
188] except in the cases documented below

5. 10. 1 Exceptions Where Coercion is Desirable

Catching both

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

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

5. 11 Disallowed features

5. 11. 1 with

Do not use the

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
191 keyword. It makes your code harder to understand and has been banned in strict mode since ES5

5. 11. 2 Dynamic code evaluation

Do not use

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
184 or the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
193 constructor [except for code loaders]. These features are potentially dangerous and simply do not work in CSP environments

5. 11. 3 Automatic semicolon insertion

Always terminate statements with semicolons [except function and class declarations, as noted above]

5. 11. 4 Non-standard features

Do not use non-standard features. This includes old features that have been removed [e. g. ,

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
194], new features that are not yet standardized [e. g. , the current TC39 working draft, proposals at any stage, or proposed but not-yet-complete web standards], or proprietary features that are only implemented in some browsers. Use only features defined in the current ECMA-262 or WHATWG standards. [Note that projects writing against specific APIs, such as Chrome extensions or Node. js, can obviously use those APIs]. Non-standard language “extensions” [such as those provided by some external transpilers] are forbidden

5. 11. 5 Wrapper objects for primitive types

Never use

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
164 on the primitive object wrappers [
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
196,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
197,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
198,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
685], nor include them in type annotations

không được phép

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

The wrappers may be called as functions for coercing [which is preferred over using

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
200 or concatenating the empty string] or creating symbols

Ví dụ

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

5. 11. 6 Modifying builtin objects

Never modify builtin types, either by adding methods to their constructors or to their prototypes. Avoid depending on libraries that do this. Note that the JSCompiler’s runtime library will provide standards-compliant polyfills where possible; nothing else may modify builtin objects

Do not add symbols to the global object unless absolutely necessary [e. g. required by a third-party API]

5. 11. 7 Omitting
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
201 when invoking a constructor

Never invoke a constructor in a

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
164 statement without using parentheses
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
201

không được phép

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

Use instead

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

Omitting parentheses can lead to subtle mistakes. These two lines are not equivalent

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

6 Naming

6. 1 Rules common to all identifiers

Identifiers use only ASCII letters and digits, and, in a small number of cases noted below, underscores and very rarely [when required by frameworks like Angular] dollar signs

Give as descriptive a name as possible, within reason. Do not worry about saving horizontal space as it is far more important to make your code immediately understandable by a new reader. Do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word

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

không được phép

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

6. 2 Rules by identifier type

6. 2. 1 Package names

Package names are all

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501. For example,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
205, but not
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
206 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
207

6. 2. 2 Class names

Class, interface, record, and typedef names are written in

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
208. Unexported classes are simply locals. they are not marked
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 and therefore are not named with a trailing underscore

Type names are typically nouns or noun phrases. For example,

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
210,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
211, or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
212. Additionally, interface names may sometimes be adjectives or adjective phrases instead [for example,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
213]

6. 2. 3 Method names

Method names are written in

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501. Names for
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 methods must end with a trailing underscore

Method names are typically verbs or verb phrases. For example,

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
216 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
217. Getter and setter methods for properties are never required, but if they are used they should be named
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
218 [or optionally
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
219 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
220 for booleans], or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
221 for setters

Underscores may also appear in JsUnit test method names to separate logical components of the name. One typical pattern is

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
222, for example
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
223. There is no One Correct Way to name test methods

6. 2. 4 Enum names

Enum names are written in

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
208, similar to classes, and should generally be singular nouns. Individual items within the enum are named in
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
225

6. 2. 5 Constant names

Constant names use

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
225. all uppercase letters, with words separated by underscores. There is no reason for a constant to be named with a trailing underscore, since private static properties can be replaced by [implicitly private] module locals

6. 2. 5. 1 Definition of “constant”

Every constant is a

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 static property or a module-local
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633 declaration, but not all
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 static properties and module-local
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633s are constants. Before choosing constant case, consider whether the field really feels like a deeply immutable constant. For example, if any of that instance's observable state can change, it is almost certainly not a constant. Merely intending to never mutate the object is generally not enough

ví dụ

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

Constants’ names are typically nouns or noun phrases

6. 2. 5. 2 Local aliases

Local aliases should be used whenever they improve readability over fully-qualified names. 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. Aliases may also be used within functions. Aliases must be
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633

ví dụ

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

6. 2. 6 Non-constant field names

Non-constant field names [static or otherwise] are written in

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501, with a trailing underscore for private fields

These names are typically nouns or noun phrases. For example,

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

6. 2. 7 tên tham số

Parameter names are written in

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501. Note that this applies even if the parameter expects a constructor

One-character parameter names should not be used in public methods

Exception. When required by a third-party framework, parameter names may begin with a

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
237. This exception does not apply to any other identifiers [e. g. local variables or properties]

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501, ngoại trừ các hằng số cục bộ mô-đun [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
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501. Lưu ý rằng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501 được sử dụng ngay cả khi biến chứa một 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ư

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
241 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
242

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 Camel case. xác định

Sometimes there is more than one reasonable way to convert an English phrase into camel case, such as when acronyms or unusual constructs like IPv6 or iOS are present. Để 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. Convert the phrase to plain ASCII and remove any apostrophes. For example, Müller's algorithm might become Muellers algorithm
  2. Divide this result into words, splitting on spaces and any remaining punctuation [typically hyphens]
    1. Recommended. if any word already has a conventional camel case appearance in common usage, split this into its constituent parts [e. g. , AdWords becomes ad words]. Note that a word such as iOS is not really in camel case per se; it defies any convention, so this recommendation does not apply
  3. Now lowercase everything [including acronyms], then uppercase only the first character of
    1. … each word, to yield upper camel case, or
    2. … each word except the first, to yield lower camel case
  4. Finally, join all the words into a single identifier

Note that the casing of the original words is almost entirely disregarded

ví dụ

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

*Acceptable, but not recommended

Note. Some words are ambiguously hyphenated in the English language. for example nonempty and non-empty are both correct, so the method names checkNonempty and checkNonEmpty are likewise both correct

7 JSDoc

JSDoc is used on all classes, fields, and methods

7. 1 General form

The basic formatting of JSDoc blocks is as seen in this example

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

or in this single-line example

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

If a single-line comment overflows into multiple lines, it must use the multi-line style with

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
244 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
245 on their own lines

Many tools extract metadata from JSDoc comments to perform code validation and optimization. As such, these comments must be well-formed

7. 2 Markdown

JSDoc is written in Markdown, though it may include HTML when necessary

Note that tools that automatically extract JSDoc [e. g. JsDossier] will often ignore plain text formatting, so if you did this

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

it would come out like this

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

Google style allows a subset of JSDoc tags. See ?? for the complete list. 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

Simple tags that do not require any additional data [such as

/* 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,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
248,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
249] may be combined onto the same line, along with an optional type when appropriate

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

There is no hard rule for when to combine tags, or in which order, but be consistent

For general information about annotating types in JavaScript see Annotating JavaScript for the Closure Compiler and Types in the Closure Type System

7. 4 Line wrapping

Line-wrapped block tags are indented four spaces. Wrapped description text may be lined up with the description on previous lines, but this horizontal alignment is discouraged

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

Do not indent when wrapping a

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

A file may have a top-level file overview. A copyright notice , author information, and default visibility level are optional. File overviews are generally recommended whenever a file consists of more than a single class definition. The top level comment is designed to orient readers unfamiliar with the code to what is in this file. If present, it may provide a description of the file's contents and any dependencies or compatibility information. Wrapped lines are not indented

Ví dụ

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

Classes, interfaces and records must be documented with a description and any template parameters, implemented interfaces, visibility, or other appropriate tags. The class description should provide the reader with enough information to know how and when to use the class, as well as any additional considerations necessary to correctly use the class. Textual descriptions may be omitted on the constructor.

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
252 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
588 annotations are not used with the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
697 keyword unless the class is being used to declare an
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
105 or it extends a generic class

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

All enums and typedefs must be documented with appropriate JSDoc tags [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
256 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
673] on the preceding line. Public enums and typedefs must also have a description. Individual enum items may be documented with a JSDoc comment on the preceding line

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

Typedefs are useful for defining short record types, or aliases for unions, complex functions, or generic types. Typedefs should be avoided for record types with many fields, since they do not allow documenting individual fields, nor using templates or recursive references. For large record types, prefer

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

In methods and named functions, parameter and return types must be documented, except in the case of same-signature

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
259s, where all types are omitted. The
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 type should be documented when necessary. Return type may be omitted if the function has no non-empty
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
621 statements

Method, parameter, and return descriptions [but not types] may be omitted if they are obvious from the rest of the method’s JSDoc or from its signature

Method descriptions begin with a verb phrase that describes what the method does. This phrase is not an imperative sentence, but instead is written in the third person, as if there is an implied This method . before it

If a method overrides a superclass method, it must include an

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
259 annotation. Overridden methods inherit all JSDoc annotations from the super class method [including visibility annotations] and they should be omitted in the overridden method. However, if any type is refined in type annotations, all
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
263 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
264 annotations must be specified explicitly

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

If you only need to document the param and return types of a function, you may optionally use inline JSDocs in the function's signature. These inline JSDocs specify the return and param types without tags

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

If you need descriptions or tags, use a single JSDoc comment above the method. For example, methods which return values need a

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

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

In anonymous functions annotations are generally optional. If the automatic type inference is insufficient or explicit annotation improves readability, then annotate param and return types like this

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

For function type expressions, see ??

Property types must be documented. The description may be omitted for private properties, if name and type provide enough documentation for understanding the code

Publicly exported constants are commented the same way as properties

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

7. 10 Type annotations

Type annotations are found on

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
263,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
264,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
182, and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
269 tags, and optionally on
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
249, and any visibility tags. Type annotations attached to JSDoc tags must always be enclosed in braces

7. 10. 1 Nullability

The type system defines modifiers

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
272 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
273 for non-null and nullable, respectively. These modifiers must precede the type

Nullability modifiers have different requirements for different types, which fall into two broad categories

  1. Type annotations for primitives [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    274,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    275,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    276,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    277,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    645,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    189] and literals [
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    280 and
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    281] are always non-nullable by default. Use the
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    273 modifier to make it nullable, but omit the redundant
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    272
  2. Reference types [generally, anything in
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    208, including
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    285] refer to a class, enum, record, or typedef defined elsewhere. Since these types may or may not be nullable, it is impossible to tell from the name alone whether it is nullable or not. Always use explicit
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    273 and
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    272 modifiers for these types to prevent ambiguity at use sites

Bad

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

Good

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

7. 10. 2 Type Casts

In cases where the compiler doesn't accurately infer the type of an expression, and the assertion functions in goog. asserts cannot remedy it , it is possible to tighten the type by adding a type annotation comment and enclosing the expression in parentheses. Note that the parentheses are required

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

7. 10. 3 Template Parameter Types

Luôn chỉ định tham số mẫu. This way compiler can do a better job and it makes it easier for readers to understand what code does

Bad

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

Good

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

Cases when template parameters should not be used

  • /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    650 is used for type hierarchy and not as map-like structure

7. 10. 4 Function type expressions

Terminology Note. function type expression refers to a type annotation for function types with the keyword

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
601 in the annotation [see examples below]

Where the function definition is given, do not use a function type expression. Specify parameter and return types with

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
263 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
264, or with inline annotations [see ??]. This includes anonymous functions and functions defined and assigned to a const [where the function jsdoc appears above the whole assignment expression]

Function type expressions are needed, for example, inside

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
256,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
263 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
264. Use it also for variables or properties of function type, if they are not immediately initialized with the function definition

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

When using a function type expression, always specify the return type explicitly. Otherwise the default return type is unknown [

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
273], which leads to strange and unexpected behavior, and is rarely what is actually desired

Bad - type error, but no warning given

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

Good

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

7. 10. 5 Whitespace

Within a type annotation, a single space or line break is required after each comma or colon. Additional line breaks may be inserted to improve readability or avoid exceeding the column limit. These breaks should be chosen and indented following the applicable guidelines [e. g. ?? and ??]. No other whitespace is allowed in type annotations

Good

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

Bad

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

7. 11 Visibility annotations

Visibility annotations [

/* 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';
679,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
678] may be specified in a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
70 block, or on any exported symbol or property. Do not specify visibility for local variables, whether within a function or at the top level of a module. All
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 names must end with an underscore

8 Policies

8. 1 Issues unspecified by Google Style. Be Consistent

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

8. 2 Compiler warnings

8. 2. 1 Use a standard warning set

As far as possible projects should use

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

8. 2. 2 How to handle a warning

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

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

  1. First, fix it or work around it. Make a strong attempt to actually address the warning, or find another way to accomplish the task that avoids the situation entirely
  2. Otherwise, determine if it's a false alarm. 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
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    302
  3. Otherwise, leave a TODO comment. This is a last resort. If you do this, do not suppress the warning. The warning should be visible until it can be taken care of properly

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

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

Ví dụ

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

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

Mark deprecated methods, classes or interfaces with

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
303 annotations. A deprecation comment must include simple, clear directions for people to fix their call sites

8. 4 Code not in Google Style

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

8. 4. 1 Reformatting existing code

When updating the style of existing code, follow these guidelines

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

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

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

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

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

8. 5 Local style rules

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

8. 6 Generated code. mostly exempt

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

9 Appendices

9. 1 JSDoc tag reference

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

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

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

9. 1. 2 Documentation annotations

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

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. These annotations appear in the JSDoc Toolkit Tag Reference but are not considered part of valid Google style

9. 1. 2. 1
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
304 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
305 - Not recommended

Not recommended

Syntax.

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
70 comment. The
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
305 tag is used by the unit test dashboard to determine who owns the test results

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

Syntax.

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

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

Indicates what bugs the given test function regression tests

Multiple bugs should each have their own

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
309 line, to make searching for regression tests as easy as possible

9. 1. 2. 3
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
312 - Deprecated. Do not use

Deprecated. Do not use. Use Markdown backticks instead

Syntax.

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

Historically,

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
314 was written as
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
315

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

Syntax.

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

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

Syntax.

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

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

Syntax.

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

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

Syntax.

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

/* 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
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
324 for Angular 1
9. 1. 3. 2
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
325 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
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
326 - Deprecated. Do not use

Deprecated. Do not use. Use

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
249 and/or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
687 instead

9. 1. 4. 2
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
329 - Deprecated. Do not use

Deprecated. Do not use. Use

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
259 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
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    304 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
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    581, 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
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
571

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
571 is compliant with the style guide

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
184 or assigning to
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
337], 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

Phần này mô tả các ngoại lệ và quy tắc bổ sung cần tuân theo khi cú pháp ECMAScript 6 hiện đại không có sẵn cho tác giả mã. Exceptions to the recommended style are required when ECMAScript 6 syntax is not possible and are outlined here

  • Use of
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    637 declarations is allowed
  • Use of
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    131 is allowed
  • Optional parameters without default values are allowed

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

9. 4. 2. 1
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 declarations are NOT block-scoped

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 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
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 declarations inside of loops. The following code gives an example

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

Even though

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 declarations are scoped to the beginning of the enclosing function,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 declarations should be as close as possible to their first use, for readability purposes. However, do not put a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 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';
399. 4. 2. 3 Use @const for constants variables

For global declarations where the

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633 keyword would be used, if it were available, annotate the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 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

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

9. 4. 4. 1 Summary

WARNING.

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 dependency management is deprecated. All new files, even in projects using
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 for older files, should use
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66. The following rules are for pre-existing
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 files only

  • Place all
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    518s 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
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    518 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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 statements should be grouped together and placed first. All
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 statements should follow. The two lists should be separated with an empty line

Similar to import statements in other languages,

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 statements should be written in a single line, even if they exceed the 80 column line length limit

The lines should be sorted alphabetically, with uppercase letters coming first

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

All members defined on a class should be in the same file. Only top-level classes should be provided in a file that contains multiple members defined on the same class [e. g. enums, inner classes, etc]

Do this

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

Not this

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

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';
459. 4. 4. 2 Aliasing with
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364

WARNING.

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 is deprecated. New files should not use
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 even in projects with existing
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 usage

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 may be used to shorten references to namespaced symbols in code using
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518/
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 dependency management

Only one

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 invocation may be added per file. Always place it in the global scope

The opening

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
372 invocation must be preceded by exactly one blank line and follow any
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 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. Nối
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
375 vào câu lệnh kết thúc của phạm vi. Tách nhận xét khỏi dấu chấm phẩy bằng hai dấu cách

Tương tự như không gian tên C++, không thụt lề dưới khai báo

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364. Thay vào đó, hãy tiếp tục từ cột 0

Chỉ tạo bí danh cho những tên sẽ không được gán lại cho đối tượng khác [e. g. , hầu hết các hàm tạo, enum và không gian tên]. Đừng làm điều này [xem bên dưới để biết cách đặt bí danh cho hàm tạo]

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

Tên phải giống với thuộc tính cuối cùng của toàn cầu mà chúng đang đặt bí danh

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

Thích sử dụng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 thay vì
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
525 để phá vỡ sự phụ thuộc vòng tròn giữa các tệp trong cùng một thư viện. Không giống như
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74, một câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 được phép nhập một không gian tên trước khi nó được xác định

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
525 vẫn có thể được sử dụng trong mã kế thừa để phá vỡ các tham chiếu vòng tròn trải dài qua các ranh giới thư viện, nhưng mã mới hơn nên được cấu trúc để tránh điều đó

Các câu lệnh của

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
525 phải tuân theo các quy tắc về kiểu dáng giống 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. The entire block of
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
525,
/* 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

Chủ Đề