1. Basic format
Indentation
It is recommended to have 4 spaces per level. You can set tab = 4 spaces for the editor , automatic conversion
Semicolon
Do not omit the semicolon to prevent ASI (automatic semicolon insertion) errors
Line width
Each line of code should not exceed 80 characters. If it is too long, you should use the operator to manually break the line
Line break
The operator is at the end of the previous line. And the next line is indented 2 levels. If it is an assignment statement, it should also be aligned with the part after the equal sign
Empty line
Function Declaration and Function declarations, variable declarations, function declarations, and logic blocks within functions should all be separated by blank lines
The author Nicholas also recommends leaving a blank line at the top of the Flow control block, but The example given is not very clear
Naming
Variable name/Function name: Camel (camel case) rule, first The first letter of a word is lowercase, the first letter of subsequent words is uppercase, and the rest is lowercase
ConstantName:C language formula, all capitals, underscore word segmentation
Constructor: Pascal rule, all the first letters of words are capitalized, and the rest are lowercase
##Literal
String : wrapped in double quotes, use the [+] operator for line breaks, do not use the \ escape character
- Value: Do not omit the part before and after the decimal point, do not use octal form
Null: only treat null as Object Placeholder, do not use it to detect formal parameters or uninitialized variables
- Undefined: All
objects should be initialized to null to Distinguish between undefined and uninitialized
- Object literal/array literal: Do not declare objects and arrays in constructor mode
Notes
P.S. There is a very classic explanation in the book:Appropriately written comments help tell the story of code,allowing other developers to drop into a part of the story without needing to hear the beginning.
Single-line comments
- End of line: use 1 Level indentation separates code, and there must be a space after //
- is on one line: used to comment below, and must maintain the same indentation as the commented code
- Head of line: used to comment multi-line code
Multi-line comments
Used to wrap large comments , recommendEclipse style, such as
/* * comment line1 * comment line2 */
Note:
- Leave a blank line above the multi-line comment
- *Leave a space after the asterisk
- Multi-line comments must be at least three lines (because there are no comments after the first and last lines)
Where to add comments
- Non-self-explanatory code
- Intentional , but it looks like something is wrong
- Browser hack
Documentation comments
Comments should be added to each function, including function description, parameters, return values, errors thrown, etc., such as the recommended Eclipse style:/** * 添加指定元素到默认数组 * * @method add * @param {Number} 将要添加的元素 * @return {Boolean} 添加成功/失败 * @throw {TypeError} 参数类型不匹配 */ function add(item){ if(typeof item === "number"){ arr.push(item) } else{ throw new TypeError(); } }3. Statements and expressions
Brace alignment
Recommended line ending style, not recommended sub-line styleBlock statement space
after if There is a space before and after the parentheses, for example:if (expr) { code }
- Indentation: case and switch alignment,
breakIndent 1 level
- case penetration: Use a blank line or comment //falls through to indicate that case penetration is intentional
- default: Keep default or use the comment //no default to indicate that there is no default
with statement
No needAll variables should be declared at the top of the function body, including variables used in the initialization part of the for loop, to avoid bugs caused by hosting (promotion) (global variables may be blocked)
for-in loop
Do not use it to traverse arrays. When using it, remember to add hasOwnProperty filtering. If you deliberately traverse prototype properties, you should use comments to explain
IV. Variables, functions, operators
Variable declaration
Function body = variable declaration + function declaration + logical statement. Separate each part with a blank line
Function declaration
Declare first and then use it. Never put the function declaration in the if branch, because browsers understand it differently. Moreover, ES does not provide standard
function calls.
There are no spaces before and after the parentheses to avoid confusion with block statements
## Anonymous functionImmediate execution
Wrap the immediately executed anonymous function in parentheses to avoid confusion with the anonymous function declarationStrict mode
Don’t enable strict mode in the global scope, only enable it inside the function. If you enable it for multiple functions, you can use anonymous functions to immediately execute the scope that limits the strict mode.Judgment is equal to
Only use === and !==eval
Do not use eval() and new Function(), use anonymous function to optimize setTimeout() And setInterval()Basic packaging type
Do not use new Boolean(), new String(), new Number()
The above is the detailed content of Detailed introduction to JavaScript coding style guide. For more information, please follow other related articles on the PHP Chinese website!

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools