Innerhtml dòng mới

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 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 bên trong

Show

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 chuỗi thoát đặ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 ________ 061, ________ 062, hoặc ________ 063). 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

gợi ý. 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';

gợi ý. Đừ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 thực hiện tập tin

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ó

hồ ??

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à các thư mục con của các 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 Imports

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 Import paths

ES module files must use the

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
68 statement to import other ES module files. Đừ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

The

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
51 file extension is not optional in import paths and must always be included

/* 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';
1
3. 4. 1. 3 Naming imports
3. 4. 1. 3. 1 Naming module imports

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 Naming default imports

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

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

cấp. 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 tệp hoặc đường dẫn của mô-đun đã nhập trong bí danh kết quả

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

3. 4. 2 Exports

Symbols are only exported if they are meant to be used outside the module. Non-exported module-local symbols are not declared

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 nor do their names end with an underscore. 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ã. You can apply the

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
69 keyword to a declaration, or use the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
507 syntax

Do not use default exports. 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';
9
3. 4. 2. 2 Xuất các lớp và đối tượng vùng chứa tĩnh

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

/* 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';
51
3. 4. 2. 3 Mutability of exports

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';
53
3. 4. 2. 4 export from

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 Circular Dependencies in ES modules

Do not create cycles between ES modules, even though the ECMAScript specification allows this. Note that it is possible to create cycles with both the

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

/* 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 tài liệu 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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 in ES modules works as it does in
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 files. You can require any Closure namespace symbol (i. e. , symbols created by
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66) and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 will return the value

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
59
3. 4. 4. 3 Declaring Closure Module IDs in ES modules

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
521 can be used within ES modules to declare a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66-like module ID. This means that this module ID can be
/* 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, etc. as if it were a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 that did not call
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
81. It does not create the module ID as a globally available JavaScript symbol

A

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 (or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
524) for a module ID from
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
521 will always return the module object (as if it was
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
504'd). As a result, the argument to
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
521 should always end with a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
533

Note. 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. There is no direct way to associate a legacy namespace with an ES module

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
521 should only be used to upgrade Closure files to ES modules in place, where named exports are used

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

In a

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

In an ES module the

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
68 statements may optionally be followed by a call to
/* 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

Imports are done with

/* 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. The names imported by a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 statement may be used both in code and in type annotations, while those imported by a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 may be used in type annotations only

The

/* 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 form a contiguous block with no empty lines. This block follows the
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 declaration separated by a single empty line. The entire argument to
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 is a namespace defined by a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66 in a separate file.
/* 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 may not appear anywhere else in the file

Each

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 is assigned to a single constant alias, or else destructured into several constant aliases. These aliases are the only acceptable way to refer to dependencies in type annotations or code. Fully qualified namespaces must not be used anywhere, except as an argument to
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75

Exception. 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ã

Aliases must match the final dot-separated component of the imported module's namespace

Exception. In certain cases, additional components of the namespace can be used to form a longer alias. 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ó. Longer aliases may be used to disambiguate otherwise identical aliases, or if it significantly improves readability. In addition, a longer alias must be used to prevent masking native types such as

/* 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, and
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
565 (for a more complete list, see Standard Built-in Objects and Web APIs at 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. If the imported name is used both in code and in type annotations, it should be imported by a single
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 statement

If a module is imported only for its side effects, the call must be a

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 (not a
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75) and assignment may be omitted. A comment is required to explain why this is needed and suppress a compiler warning

Các dòng được sắp xếp theo các quy tắc sau. All requires with a name on the left hand side come first, sorted alphabetically by those names. Sau đó, yêu cầu phá hủy, được sắp xếp lại theo tên ở phía bên trái. Finally, any require calls that are standalone (generally these are for modules imported just for their side effects)

Tip. Không cần phải ghi nhớ lệnh này và thực thi thủ công. You can rely on your IDE to report requires that are not sorted correctly

If a long alias or module name would cause a line to exceed the 80-column limit, it must not be wrapped. require lines are an exception to the 80-column limit

Ví dụ

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

Discouraged

/* 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 The file’s implementation

The actual implementation follows after all dependency information is declared (separated by at least one blank line)

This may consist of any module-local declarations (constants, variables, classes, functions, etc), as well as any exported symbols

4 Định dạng

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

gợi ý. 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 định dạng clang 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

Braces are required for all control structures (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, as well as any others), even if the body contains only a single statement. 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ệ. A simple if statement that can fit entirely on a single line with no wrapping (and that doesn’t have an else) may be kept on a single line with no braces when it improves readability. This is the only case in which a control structure may omit braces and newlines

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

4. 1. 2 Nonempty blocks. phong cách K&R

Braces follow the Kernighan and Ritchie style (Egyptian brackets) for nonempty blocks and block-like constructs

  • Không ngắt dòng trước dấu ngoặc mở
  • Ngắt dòng sau cú đúp mở đầu
  • Ngắt dòng trước dấu ngoặc nhọn
  • Ngắt dòng sau dấu ngoặc nhọn nếu dấu ngoặc nhọn đó kết thúc câu lệnh hoặc phần thân của hàm hoặc câu lệnh lớp hoặc phương thức lớp. Cụ thể, không có ngắt dòng sau dấu ngoặc nhọn nếu theo sau nó là ________ 3574, ________ 3579, ________ 3577 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 or
/* 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. nhận dạng 2 khối. +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ự của lớp (dù là khai báo hay biểu thức) được dự định là các 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 templated

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 switch

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
589 và trường hợp sau

Ví dụ

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

4. 3 tuyên bố

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

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

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

Mọi câu lệnh phải được kết thúc bằng dấu chấm phẩy. Dựa vào chèn dấu chấm phẩy tự động bị cấm

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

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

ngoại lệ

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

4. 5 Line-wrapping

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

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

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

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

ưa thích

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

Discouraged

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

Trong ví dụ trước, các mức cú pháp từ cao nhất đến thấp nhất như sau. gán, chia, gọi hàm, tham số, hằng số

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

  1. Khi một dòng bị ngắt tại một toán tử, dấu ngắt xuất hiện sau ký hiệu. (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. Tên phương thức hoặc hàm tạo được gắn vào dấu ngoặc đơn mở (
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    596) theo sau nó
  3. Dấu phẩy (
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    597) vẫn được gắn vào mã thông báo đứng trước nó

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

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

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

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

??

4. 6 khoảng trắng

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

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

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

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

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

Việc sử dụng khoảng trắng ngang phụ thuộc vào vị trí và thuộc ba loại chính. đầu (ở đầu dòng), theo sau (ở cuối dòng) và nội bộ. Khoảng trắng hàng đầu (i. e. , thụt đầu dòng) được giải quyết ở nơi khác. Khoảng trắng ở cuối bị cấm

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

  1. Tách bất kỳ từ dành riêng nào (chẳng hạn như
    /* 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 hoặc
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    579) ngoại trừ
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    601 và
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    602, khỏi dấu ngoặc đơn mở (
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    596) theo sau từ đó trên dòng đó
  2. Tách bất kỳ từ dành riêng nào (chẳng hạn như
    /* 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';
    
    579) khỏi dấu ngoặc nhọn đóng (
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    606) đứng trước từ đó trên dòng đó
  3. Trước bất kỳ dấu ngoặc nhọn mở nào (______3607), với hai ngoại lệ
    1. Trước một ký tự đối tượng là đối số đầu tiên của một hàm hoặc phần tử đầu tiên trong một ký tự mảng (e. g.
      /* Poor: the reader has no idea what character this is. */
      const units = '\u03bcs';
      
      608)
    2. Trong bản mở rộng mẫu, vì nó bị cấm bởi ngôn ngữ (e. g. có hiệu lực.
      /* Poor: the reader has no idea what character this is. */
      const units = '\u03bcs';
      
      609, bị vô hiệu hóa.
      /* Poor: the reader has no idea what character this is. */
      const units = '\u03bcs';
      
      610)
  4. Trên cả hai mặt của bất kỳ toán tử nhị phân hoặc bậc ba nào
  5. Sau dấu phẩy (
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    597) hoặc dấu chấm phẩy (
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    612). Lưu ý rằng khoảng cách không bao giờ được phép trước các ký tự này
  6. Sau dấu hai chấm (
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    613) trong một đối tượng theo nghĩa đen
  7. Ở cả hai bên của dấu gạch chéo kép (
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    614) bắt đầu nhận xét cuối dòng. Ở đây, nhiều khoảng trắng được cho phép, nhưng không bắt buộc
  8. Sau một ký tự bình luận khối mở và ở cả hai bên của các ký tự đóng (e. g. cho các khai báo kiểu dạng ngắn, ép kiểu và nhận xét tên tham số.
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    615;

4. 6. 3 Căn ngang. chán nản

Lưu ý thuật ngữ. Căn chỉnh theo chiều ngang là cách thêm một số khoảng trắng bổ sung có thể thay đổi vào mã của bạn với mục tiêu làm cho một số mã thông báo nhất định xuất hiện ngay bên dưới một số mã thông báo khác trên các dòng trước đó

Phương pháp này được cho phép, nhưng nó thường không được Google Style khuyến khích. Thậm chí không bắt buộc phải duy trì căn chỉnh ngang ở những nơi nó đã được sử dụng

Đây là một ví dụ không có căn chỉnh, theo sau là một ví dụ có căn chỉnh. Cả hai đều được phép, nhưng cái sau không được khuyến khích

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

gợi ý. Căn chỉnh có thể hỗ trợ khả năng đọc, nhưng nó tạo ra các vấn đề cho việc bảo trì trong tương lai. Xem xét một thay đổi trong tương lai chỉ cần chạm vào một dòng. Thay đổi này có thể khiến định dạng dễ chịu trước đây bị xáo trộn và điều đó được cho phép. Thông thường, nó cũng nhắc người viết mã (có thể là bạn) điều chỉnh khoảng trắng trên các dòng gần đó, có thể kích hoạt một loạt định dạng lại xếp tầng. Thay đổi một dòng đó giờ có bán kính vụ nổ. Điều này tệ nhất có thể dẫn đến công việc bận rộn vô nghĩa, nhưng tốt nhất thì nó vẫn làm hỏng thông tin lịch sử phiên bản, làm chậm người đánh giá và làm trầm trọng thêm xung đột hợp nhất

4. 6. 4 đối số chức năng

Thích đặt tất cả các đối số hàm trên cùng một dòng với tên hàm. Nếu làm như vậy sẽ vượt quá giới hạn 80 cột, các đối số phải được ngắt dòng theo cách có thể đọc được. Để tiết kiệm dung lượng, bạn có thể ngắt dòng càng gần 80 càng tốt hoặc đặt mỗi đối số trên một dòng riêng để dễ đọc hơn. Thụt đầu dòng phải là bốn khoảng trắng. Căn chỉnh theo dấu ngoặc đơn được cho phép, nhưng không được khuyến khích. Dưới đây là các mẫu phổ biến nhất để gói đối số

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

4. 7 Nhóm dấu ngoặc đơn. khuyến khích

Các dấu ngoặc nhóm tùy chọn chỉ được bỏ qua khi tác giả và người đánh giá đồng ý rằng không có khả năng mã sẽ bị hiểu sai nếu không có chúng, cũng như chúng sẽ không làm cho mã dễ đọc hơn. Sẽ không hợp lý nếu cho rằng mọi người đọc đã 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

Dấu ngoặc đơn là bắt buộc đối với kiểu phôi.

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

Nhận xét khối được xác định ở cùng cấp độ với mã xung quanh. 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. Đối với nhận xét nhiều dòng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
628, các dòng tiếp theo phải bắt đầu bằng * được căn chỉnh với
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
631 trên dòng trước đó, để làm cho nhận xét rõ ràng mà không có ngữ cảnh bổ sung

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

Nhận xét không được đặt trong các hộp được vẽ bằng dấu hoa thị hoặc các ký tự khác

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

Nhận xét "Tên tham số" nên được sử dụng bất cứ khi nào giá trị và tên phương thức không truyền đạt đầy đủ ý nghĩa và việc tái cấu trúc phương thức để rõ ràng hơn là không khả thi. Đị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 bao gồm nhiều tính năng đáng ngờ (và thậm chí nguy hiểm). Phần này mô tả những tính năng nào có thể được sử dụng hoặc không được sử dụng và mọi ràng buộc bổ sung đối với việc sử dụng chúng

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. Không được sử dụng từ khóa
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637

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

Mỗi khai báo biến cục bộ chỉ khai báo một biến. 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ể

Các biến cục bộ không được khai báo theo thói quen khi bắt đầu khối chứa hoặc cấu trúc giống như khối của chúng. Thay vào đó, các biến cục bộ được khai báo gần với điểm chúng được sử dụng lần đầu tiên (với lý do), để giảm thiểu phạm vi của chúng

5. 1. 4 Khai báo các loại theo nhu cầu

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. trình biên dịch sẽ chỉ xử lý JsDoc đầu tiên và các chú thích nội tuyến sẽ bị mất

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

gợi ý. Có nhiều trường hợp trình biên dịch có thể suy ra kiểu mẫu nhưng không thể suy ra tham số của nó. Điều này đặc biệt xảy ra khi lời gọi hàm khởi tạo hoặc hàm khởi tạo không bao gồm bất kỳ giá trị nào của loại tham số mẫu (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 Sử dụng dấu phẩy sau

Bao gồm dấu phẩy ở cuối bất cứ khi nào có dấu ngắt dòng giữa 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

Hàm tạo dễ bị lỗi nếu các đối số được thêm hoặc xóa. Sử dụng một nghĩa đen thay thế

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. nếu

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
642 là một số nguyên thì
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
643 là một mảng có kích thước
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
642 trong đó tất cả các phần tử là
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
645. Nếu
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
642 là bất kỳ số nào khác, thì một ngoại lệ sẽ được đưa ra và nếu đó là bất kỳ số nào khác thì đó sẽ là một mảng một phần tử

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
647 khi thích hợp

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
648). Thay vào đó, hãy sử dụ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';
650)

5. 2. 4 Sự hủy diệt

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

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

Việc hủy cấu trúc cũng có thể được sử dụng cho các tham số chức năng (lưu ý rằng tên tham số là bắt buộc nhưng bỏ qua). Luôn chỉ định

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
652 làm giá trị mặc định nếu tham số mảng bị hủy cấu trúc là tùy chọn và cung cấp giá trị mặc định ở phía bên trái

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

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
651) để làm phẳng các phần tử ra khỏi một hoặc nhiều lần lặp khác. 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 Không sử dụng hàm tạo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
650

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

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

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

Tên thuộc tính được tính toán (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

Note that

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 in a method shorthand or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
601 refers to the object literal itself whereas
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 in an arrow function refers to the scope outside the object literal

Ví dụ

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

5. 3. 6 Shorthand properties

Thuộc tính tốc ký được phép trên đối tượng chữ

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

Các đối tượng bị hủy cấu trúc cũng có thể được sử dụng làm tham số chức năng, nhưng phải được giữ càng đơn giản càng tốt. một cấp duy nhất của các thuộc tính tốc ký không được trích dẫn. 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. JSDoc cho tham số bị hủy cấu trúc có thể được đặt bất kỳ tên nào (tên này không được sử dụng nhưng được yêu cầu bởi trình biên dịch)

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. Các thuộc tính bổ sung có thể không được thêm vào một enum sau khi nó được xác định. 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. Các hàm tạo của lớp con phải gọi

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
674 trước khi đặt bất kỳ trường nào hoặc truy cập vào
/* 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

Đặt tất cả các trường của một đối tượng cụ thể (i. e. all properties other than methods) in the constructor. Chú thích các trường không bao giờ được gán lại với

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 (những trường này không cần phải quá bất biến). Chú thích các trường không công khai bằng chú thích khả năng hiển thị phù hợp (
/* 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) và kết thúc tên của tất cả các trường
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 bằng dấu gạch dưới. 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

gợi ý. Không bao giờ được thêm hoặc xóa các thuộc tính khỏi một phiên bản sau khi hàm tạo kết thúc, vì nó cản trở đáng kể khả năng tối ưu hóa của máy ảo. Nếu cần, các trường được khởi tạo sau này phải được đặt rõ ràng thành

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
645 trong hàm tạo để ngăn thay đổi hình dạng sau này. Thêm
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
683 vào một đối tượng sẽ kiểm tra xem các thuộc tính không được khai báo không được thêm/truy cập. Các lớp có thêm cái này theo mặc định

5. 4. 3 Thuộc tính tính toán

Các thuộc tính được tính chỉ có thể được sử dụng trong các lớp khi thuộc tính là một ký hiệu. Các thuộc tính kiểu chính tả (nghĩa là các khóa không phải ký hiệu được trích dẫn hoặc tính toán, như được định nghĩa trong ??) không được phép. Một phương thức

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
663 nên được xác định cho bất kỳ lớp nào có thể lặp lại một cách hợp lý. Beyond this,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
685 should be used sparingly

gợi ý. 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

Khi nó không ảnh hưởng đến khả năng đọc, hãy ưu tiên các hàm mô-đun cục bộ hơn các phương thức tĩnh riêng tư

Các phương thức tĩnh chỉ nên được gọi trên chính lớp cơ sở. Các phương thức tĩnh không nên được gọi trên các biến chứa một thể hiện động có thể là hàm tạo hoặc hàm tạo của lớp con (và phải được xác định bằng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
687 nếu điều này được thực hiện) và không được gọi trực tiếp trên lớp con không xác định bản thân phương pháp

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. Ví dụ

  1. Nếu tồn tại hoặc sẽ tồn tại các lớp con, bao gồm cả các khung tạo ra các lớp con, không thể thay đổi ngay lập tức để sử dụng cú pháp lớp ES6. If such a class were to use ES6 syntax, all downstream subclasses not using ES6 class syntax would need to be modified

  2. Frameworks that require a known

    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    666 value before calling the superclass constructor, since constructors with ES6 super classes do not have access to the instance
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    666 value until the call to
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    602 returns

Trong tất cả các cách khác, hướng dẫn kiểu vẫn áp dụng cho mã này.

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

Ngoài ra, mặc dù

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
693 nên được ưu tiên cho tất cả mã mới, nhưng cú pháp truyền thống hơn cũng được cho phép

/* 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. Vì lý do đó, tốt nhất là sử dụng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
695 từ Thư viện đóng cửa

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

Không sử dụng thuộc tính getter và setter của JavaScript. Chúng có khả năng gây ngạc nhiên và khó giải thích và có sự hỗ trợ hạn chế trong trình biên dịch. Thay vào đó hãy cung cấp các phương thức thông thường

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

Phương pháp

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
104 có thể bị ghi đè, nhưng phải luôn thành công và không bao giờ có tác dụng phụ có thể nhìn thấy

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

5. 4. 9 giao diện

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 Nested functions and closures

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. Thích các hàm mũi tên hơn từ khóa
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
601, đặc biệt đối với các hàm lồng nhau (nhưng hãy xem ??)

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 khác, chẳng hạn như
/* 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 và
/* 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). Dạng biểu thức đơn được ưu tiên nếu nó cải thiện khả năng đọc (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. hồ ??

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

cấp. 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

gợi ý. 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 Rest parameters

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. Tham số còn lại phải là tham số cuối cùng trong danh sách. 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 Toán tử trải rộng

Function calls may use the spread operator (

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
651). Ưu tiên toán tử trải rộng hơn là
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
138 khi một mảng hoặc khả năng lặp được giải nén thành nhiều tham số của hàm biến thiên. There is no space after the
/* 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. Tất cả có thể được sử dụng, mặc dù các vòng lặp
/* 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 nên được ưu tiên hơn khi có thể

/* 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. Ưu tiên ________ 3575-________ 3625 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
159 hơn ________ 3575-________ 3624 khi có thể

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. không bao giờ ném chuỗi ký tự hoặc các đối tượng khác. 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. They should be defined and used wherever the native

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

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

It is very rarely correct to do nothing in response to a caught exception. When it truly is appropriate to take no action whatsoever in a catch block, the reason this is justified is explained in a comment

/* 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. Unlike in some other languages, patterns like the above simply don’t work since this will catch the error thrown by

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

5. 8. 3 Switch statements

Terminology Note. Bên trong dấu ngoặc nhọn của khối chuyển đổi là một hoặc nhiều nhóm câu lệnh. Each statement group consists of one or more switch labels (either

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
172 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
173), followed by one or more statements

5. 8. 3. 1 Fall-through. nhận xét

Within a switch block, each statement group either terminates abruptly (with a

/* 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 or
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
622n exception), or is marked with a comment to indicate that execution will or might continue into the next statement group. Any comment that communicates the idea of fall-through is sufficient (typically
/* 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';
11
5. 8. 3. 2 The
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
178 case is present

Each switch statement includes a

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
178 statement group, even if it contains no code. The
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
178 statement group must be last

5. 9 cái này

Chỉ sử dụng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 trong các hàm tạo và phương thức của lớp, trong các hàm mũi tên được định nghĩa trong các hàm tạo và phương thức của lớp hoặc trong các hàm có khai báo rõ ràng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
182 trong JSDoc của hàm bao quanh ngay lập tức

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
187/______4188) ngoại trừ các trường hợp được nêu dưới đây

5. 10. 1 Exceptions Where Coercion is Desirable

Catching both

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

Không sử dụng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
184 hoặc hàm tạo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
193 (ngoại trừ trình tải mã). These features are potentially dangerous and simply do not work in CSP environments

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

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. (Lưu ý rằng các dự án viết dựa trên các API cụ thể, chẳng hạn như tiện ích mở rộng của Chrome hoặc Node. js, 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. Lưu ý rằng thư viện thời gian chạy của JSCompiler sẽ cung cấp các polyfill tuân thủ tiêu chuẩn nếu có thể;

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

5. 11. 7 Bỏ qua
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
201 khi gọi hàm tạo

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 tên

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
208, tương tự như các lớp và thường phải là danh từ số ít. 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

Tên không đổi sử dụng

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 hoặc một khai báo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633 cục bộ mô-đun, nhưng không phải tất cả các thuộc tính tĩnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96 và các
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633 cục bộ mô-đun đều là hằng số. 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. Bí danh phải là
/* 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 Parameter names

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

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

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ộ

Local variable names are written in

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501, except for module-local (top-level) constants, as described above. 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. Note that
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
501 is used even if the variable holds a constructor

6. 2. 9 Template parameter names

Template parameter names should be concise, single-word or single-letter identifiers, and must be all-caps, such as

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

6. 2. 10 Module-local names

Module-local names that are not exported are implicitly private. They are not marked

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94 and do not end in an underscore. This applies to classes, functions, variables, constants, enums, and other module-local identifiers

6. 3 Camel case. defined

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. To improve predictability, Google Style specifies the following (nearly) deterministic scheme

Beginning with the prose form of the name

  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 trở thành từ quảng cáo). Lưu ý rằng một từ chẳng hạn như iOS không thực sự ở dạng lạc đà;
  3. Bây giờ viết thường mọi thứ (bao gồm cả từ viết tắt), sau đó chỉ viết hoa ký tự đầu tiên của
    1. …mỗi từ, để tạo ra trường hợp lạc đà trên, hoặc
    2. … mỗi từ ngoại trừ từ đầu tiên, để mang lại trường hợp lạc đà thấp hơn
  4. Cuối cùng, nối tất cả các từ thành một định danh duy nhất

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

ví dụ

Mẫu văn xuôi ĐúngSaiYêu cầu HTTP XMLXmlHttpRequestXMLHTTPYêu cầuID khách hàng mớimớiCustomerIdnewCustomerIDđồng hồ bấm giờ bên trongĐồng hồ bấm giờbên trongStopWatchhỗ trợ IPv6 trên iOS?Hỗ trợIpv6OnIoshỗ trợIPv6OnIOSNhà nhập khẩu YouTubeYouTubeImporterYoutubeImporter*

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

cấp. Một số từ được gạch nối một cách mơ hồ trong ngôn ngữ tiếng Anh. for example nonempty and non-empty are both correct, so the method names checkNonempty and checkNonEmpty are likewise both correct

7 JSDoc

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

7. 1 hình thức chung

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

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

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
244 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
245 trên các dòng riêng của chúng

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

7. 2 đánh dấu

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

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

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

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

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

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

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

Kiểu Google cho phép một tập hợp con các thẻ JSDoc. hồ ?? . Hầu hết các thẻ phải chiếm dòng riêng của chúng, với thẻ ở đầu dòng

không được phép

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
94,
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
96,
/* 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) có thể được kết hợp trên cùng một dòng, cùng với một loại tùy chọn khi thích hợp

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

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

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

7. gói 4 dòng

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

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

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

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

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

Ví dụ

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
252 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
588 không được sử dụng với từ khóa
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
697 trừ khi lớp này đang được sử dụng để khai báo một
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
105 hoặc nó mở rộng một lớp chung

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
256 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
673) ở dòng trước. Công khai enums và typedefs cũng phải có một mô tả. Các mục enum riêng lẻ có thể được ghi lại bằng nhận xét JSDoc trên dòng trước

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

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
259 có cùng chữ ký, trong đó tất cả các kiểu được bỏ qua. Loại
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
666 phải được ghi lại khi cần thiết. Kiểu trả về có thể được bỏ qua nếu hàm không có câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
621 không trống

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

Mô tả phương thức bắt đầu bằng một cụm động từ mô tả chức năng của phương thức. 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. Tuy nhiên, nếu bất kỳ loại nào được tinh chỉnh trong chú thích loại, thì tất cả chú thích
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
263 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
264 phải được chỉ định rõ ràng

/* 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 loại chú thích

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

bồn tắm

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

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

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

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

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

bồn tắm

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

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

  • /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    650 được sử dụng cho phân cấp kiểu và không phải là cấu trúc giống như bản đồ

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
601 trong chú thích (xem các ví dụ bên dưới)

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
263 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
264 hoặc với chú thích nội tuyến (xem ??). Điều này bao gồm các hàm ẩn danh và các hàm được xác định và gán cho một const (trong đó hàm jsdoc xuất hiện phía trên toàn bộ biểu thức gán)

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

/* 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 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
264. Cũng sử dụng nó cho các biến hoặc thuộc tính của kiểu hàm, nếu chúng không được khởi tạo ngay với định nghĩa hàm

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

Khi sử dụng biểu thức kiểu hàm, luôn chỉ định rõ ràng kiểu trả về. Mặt khác, kiểu trả về mặc định không xác định (

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
273), dẫn đến hành vi kỳ lạ và không mong muốn, và hiếm khi là điều thực sự mong muốn

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

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

Good

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

7. 10. 5 khoảng trắng

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

Good

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

bồn tắm

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

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

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

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

8 chính sách

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

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

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

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

Các dự án càng xa càng tốt nên sử dụng

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

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

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

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

  1. Đầu tiên, sửa chữa nó hoặc làm việc xung quanh nó. Thực hiện một nỗ lực mạnh mẽ để thực sự giải quyết cảnh báo hoặc tìm cách khác để hoàn thành nhiệm vụ tránh hoàn toàn tình huống
  2. Nếu không, hãy xác định xem đó có phải là báo động giả không. Nếu bạn tin rằng cảnh báo không hợp lệ và mã thực sự an toàn và chính xác, hãy thêm nhận xét để thuyết phục người đọc về sự thật này và áp dụng chú thích
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    302
  3. Nếu không, hãy để lại nhận xét TODO. Đây là một phương sách cuối cùng. Nếu bạn làm điều này, đừng chặn cảnh báo. Cảnh báo sẽ hiển thị cho đến khi nó có thể được xử lý đúng cách

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

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

Ví dụ

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
303. Nhận xét không dùng nữa phải bao gồm hướng dẫn đơn giản, rõ ràng để mọi người khắc phục trang web cuộc gọi của họ

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

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

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

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

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

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

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

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

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

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

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

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

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

9 phụ lục

9. 1 tham chiếu thẻ JSDoc

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

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

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

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

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

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

9. 1. 2. 1
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
304 hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
305 - Không nên dùng

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

cú pháp.

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
70. Thẻ
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
305 được bảng điều khiển kiểm tra đơn vị sử dụng để xác định ai sở hữu kết quả kiểm tra

9. 1. 2. 2____5309

cú pháp.

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
309 riêng, để giúp việc tìm kiếm các bài kiểm tra hồi quy trở nên dễ dàng nhất có thể

9. 1. 2. 3
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
312 - Không dùng nữa. Không được dùng

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

cú pháp.

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

Trong lịch sử,

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
314 được viết là
/* 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

Cho biết rằng một thuật ngữ trong mô tả JSDoc là mã để nó có thể được định dạng chính xác trong tài liệu được tạo

9. 1. 2. 4____5250

cú pháp.

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

cú pháp.

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

Thẻ này được sử dụng để tạo liên kết tham chiếu chéo trong tài liệu được tạo

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

Ghi chú lịch sử. Các thẻ @link cũng đã được sử dụng để tạo các liên kết bên ngoài trong tài liệu được tạo. Đối với các liên kết bên ngoài, hãy luôn sử dụng cú pháp liên kết của Markdown để thay thế

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

cú pháp.

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

Tham chiếu tra cứu đến một hàm hoặc phương thức lớp khác

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

cú pháp.

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

Được sử dụng trong phần tổng quan về tệp để cho biết trình duyệt nào được tệp hỗ trợ

9. 1. 3 Chú thích cụ thể của khung

Các chú thích sau đây dành riêng cho một khung cụ thể

9. 1. 3. 1
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
324 cho Góc 1
9. 1. 3. 2
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
325 cho Polyme

https. //github. com/google/closure-compiler/wiki/polymer-pass

9. 1. 4 Lưu ý về chú thích Trình biên dịch đóng cửa tiêu chuẩn

Các thẻ sau từng là tiêu chuẩn nhưng hiện không được dùng nữa

9. 1. 4. 1
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
326 - Không dùng nữa. Không được dùng

không dùng nữa. Không được dùng. Sử dụng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
249 và/hoặc
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
687 để thay thế

9. 1. 4. 2
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
329 - Không dùng nữa. Không được dùng

không dùng nữa. Không được dùng. Sử dụng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
259 để thay thế

9. 2 Quy tắc phong cách thường bị hiểu lầm

Đây là tập hợp các sự kiện ít được biết đến hoặc thường bị hiểu lầm về Google Style cho JavaScript. (Sau đây là những tuyên bố đúng; đây không phải là danh sách những huyền thoại. )

  • Không yêu cầu tuyên bố bản quyền cũng như tín dụng
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    304 trong tệp nguồn. (Cả hai đều không được khuyến nghị rõ ràng. )
  • Không có quy tắc cứng và nhanh nào quản lý cách sắp xếp thứ tự các thành viên của một lớp (??)
  • Các khối trống thường có thể được biểu diễn ngắn gọn dưới dạng
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    581, như chi tiết trong (??)
  • Chỉ thị chính của line-wrapping là. thích ngắt ở cấp độ cú pháp cao hơn (??)
  • Các ký tự không phải ASCII được cho phép ở dạng chuỗi ký tự, nhận xét và JSDoc, và trên thực tế được khuyến nghị khi chúng làm cho mã dễ đọc hơn so với lối thoát Unicode tương đương (??)

Các công cụ sau tồn tại để hỗ trợ các khía cạnh khác nhau của Google Style

9. 3. 1 trình biên dịch đóng

Chương trình này thực hiện kiểm tra kiểu và các kiểm tra khác, tối ưu hóa và các chuyển đổi khác (chẳng hạn như giảm mã ECMAScript 6 xuống ECMAScript 5)

9. 3. 2____3571

Chương trình này định dạng lại mã nguồn JavaScript thành Google Style và cũng tuân theo một số phương pháp định dạng nâng cao khả năng đọc không bắt buộc nhưng thường xuyên. Đầu ra do

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
571 tạo ra tuân thủ hướng dẫn về kiểu dáng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
571 không bắt buộc. Các tác giả được phép thay đổi đầu ra của nó và người đánh giá được phép yêu cầu những thay đổi đó; . Tuy nhiên, các cây con có thể chọn tham gia thực thi như vậy cục bộ

9. 3. 3 Trình biên dịch đóng cửa

Chương trình này kiểm tra một loạt các bước sai và chống mẫu

9. 3. 4 khung tuân thủ

JS Conformance Framework là một công cụ nằm trong Closure Compiler cung cấp cho các nhà phát triển một phương tiện đơn giản để chỉ định một tập hợp các kiểm tra bổ sung sẽ được chạy đối với cơ sở mã của họ trên các kiểm tra tiêu chuẩn. Ví dụ: kiểm tra sự phù hợp có thể cấm truy cập vào một thuộc tính nhất định hoặc lệnh gọi đến một chức năng nhất định hoặc thông tin loại bị thiếu (không xác định)

Các quy tắc này thường được sử dụng để thực thi các hạn chế quan trọng (chẳng hạn như xác định toàn cục, có thể phá vỡ cơ sở mã) và các mẫu bảo mật (chẳng hạn như sử dụng

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
184 hoặc gán cho
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
337) hoặc để cải thiện chất lượng mã một cách lỏng lẻo hơn

Để biết thêm thông tin, hãy xem tài liệu chính thức về Khung tuân thủ JS

9. 4 ngoại lệ cho các nền tảng cũ

9. 4. 1. Tổng quan

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ã. Các ngoại lệ đối với kiểu được đề xuất là bắt buộc khi không thể sử dụng cú pháp ECMAScript 6 và được nêu tại đây

  • Cho phép sử dụng khai báo
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    637
  • Cho phép sử dụng
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    131
  • Cho phép tham số tùy chọn không có giá trị mặc định

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

9. 4. 2. 1 khai báo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 KHÔNG có phạm vi khối

Các khai báo

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 nằm trong phạm vi bắt đầu của hàm, tập lệnh hoặc mô-đun kèm theo gần nhất, điều này có thể gây ra hành vi không mong muốn, đặc biệt là với các lần đóng hàm tham chiếu các khai báo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 bên trong các vòng lặp. Đoạn mã sau đưa ra một ví dụ

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
38
9. 4. 2. 2 Khai báo các biến càng gần với lần sử dụng đầu tiên càng tốt

Mặc dù các khai báo

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 nằm trong phạm vi bắt đầu của hàm kèm theo, các khai báo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 phải càng gần càng tốt với lần sử dụng đầu tiên của chúng, vì mục đích dễ đọc. Tuy nhiên, không đặt khai báo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 bên trong khối nếu biến đó được tham chiếu bên ngoài khối. Ví dụ

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
39
9. 4. 2. 3 Sử dụng @const cho các biến hằng

Đối với các khai báo toàn cầu nơi từ khóa

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
633 sẽ được sử dụng, nếu có, hãy chú thích khai báo
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 bằng @const thay thế (đây là tùy chọn cho các biến cục bộ)

9. 4. 3 Không sử dụng khai báo hàm phạm vi khối

Đừng làm điều này

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

Mặc dù hầu hết các máy ảo JavaScript được triển khai trước khi ECMAScript 6 hỗ trợ khai báo hàm trong các khối nhưng nó không được chuẩn hóa. Việc triển khai không nhất quán với nhau và với hành vi ECMAScript 6 tiêu chuẩn hiện nay để khai báo hàm phạm vi khối. ECMAScript 5 trở về trước chỉ cho phép khai báo hàm trong danh sách câu lệnh gốc của tập lệnh hoặc hàm và cấm rõ ràng chúng trong phạm vi khối ở chế độ nghiêm ngặt

Để có được hành vi nhất quán, thay vào đó, hãy sử dụng một

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
637 được khởi tạo với một biểu thức hàm để xác định một hàm trong một khối

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

9. 4. 4 Quản lý phụ thuộc với
/* 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. Tóm tắt

CẢNH BÁO. Quản lý phụ thuộc

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 không được dùng nữa. Tất cả các tệp mới, ngay cả trong các dự án sử dụng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 cho các tệp cũ hơn, nên sử dụng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
66. Các quy tắc sau đây chỉ dành cho các tệp
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 có sẵn

  • Đặt tất cả các
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    518 trước,
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    74 thứ hai. Tách cung cấp khỏi yêu cầu bằng một dòng trống
  • Sắp xếp các mục theo thứ tự bảng chữ cái (viết hoa trước)
  • Không bọc các câu lệnh
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    518 và
    /* Poor: the reader has no idea what character this is. */
    const units = '\u03bcs';
    
    74. Vượt quá 80 cột nếu cần
  • Chỉ cung cấp các biểu tượng cấp cao nhất

Các câu lệnh

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518 nên được nhóm lại với nhau và đặt trước. Tất cả các câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 phải tuân theo. Hai danh sách phải được phân tách bằng một dòng trống

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

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

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

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

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

Làm cái này

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

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

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

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

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
45
9. 4. 4. 2 bí danh với
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364

CẢNH BÁO.

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 không được dùng nữa. Các tệp mới không nên sử dụng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 ngay cả trong các dự án có sử dụng
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 hiện có

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 có thể được sử dụng để rút ngắn các tham chiếu đến các ký hiệu được đặt tên trong mã bằng cách sử dụng quản lý phụ thuộc
/* 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

Chỉ có thể thêm một lệnh gọi

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
364 cho mỗi tệp. Luôn đặt nó trong phạm vi toàn cầu

Lời gọi

/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
372 mở đầu phải được đặt trước chính xác một dòng trống và theo sau mọi câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
518, câu lệnh
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
74 hoặc nhận xét cấp cao nhất. Lệnh gọi phải được đóng ở dòng cuối cùng trong tệp. 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 các 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';
47
9. 4. 4. 3____3525

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. Toàn bộ khối câu lệnh
/* 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 và
/* Poor: the reader has no idea what character this is. */
const units = '\u03bcs';
75 được sắp xếp theo thứ tự bảng chữ cái