Home > Web Front-end > JS Tutorial > Today's most popular JavaScript code specifications_Basic knowledge

Today's most popular JavaScript code specifications_Basic knowledge

WBOY
Release: 2016-05-16 16:56:14
Original
1010 people have browsed it

What are the best JavaScript code programming practices? This may be a difficult question to answer. So, let’s change the question, what coding standards are the most popular?

sideeffect.kr came up with some interesting results by analyzing open source code hosted on GitHub. Let’s take a look.

Comma at the end of the line versus comma at the beginning of the line
Quotation mark at the end of the line:

Copy code The code is as follows:

var foo = 1,
bar = 2,
baz = 3;

var obj = {
foo: 1,
bar: 2,
baz: 3
};

First line quotation mark:
Copy code The code is as follows:

var foo = 1
, bar = 2
, baz = 3;

var obj = {
foo: 1
, bar: 2
, baz: 3
};

End of line, 92.345%; beginning of line, 7.655%. (Based on 1,100,251 submissions.)

Spaces and Tabs
Everyone loves to use spaces these days. Using space indentation can ensure that different developers and different editor settings see the same results.

Space, 81.1%; Tab, 18.9%. (Based on 2,019,550 submissions.)

Whether to add spaces after the function
No spaces

Copy code The code is as follows:

function foo() {
return "bar";
}

has spaces
Copy the code The code is as follows:

function foo () {
return "bar";
}

None Spaces, 67.424%; spaces, 32.576%. (Based on 1,212,488 submissions.)

Is there a space between the parameters and the brackets?
No space

Copy the code The code is as follows:

function fn(arg1, arg2) {
//or
if (true) {

has spaces
Copy code The code is as follows:

function fn( arg1, arg2 ) {
// ...
}

if ( true ) {
// ...
}

Without spaces, 94.31%; with spaces, 5.69%. (Based on 1,514,971 submissions.)

Is there a space around the colon in the object literal?
Is there a space after the colon? > The code is as follows:

Nothing after the colon Space




Copy code

The code is as follows:
There are spaces before and after the colon




Copy code

The code is as follows:
Trailing space, 62.955%; no space, 22.891%; leading and trailing spaces, 14.154%. (Based on 1,300,035 submissions.)

Personally, I feel that no spaces are too crowded, which is not conducive to quickly distinguishing key and value. If there are spaces before and after, I'm afraid you need to align the colons to make it look beautiful. Judging from statistics, most programmers are too lazy to align colons (or is it that most programmers' IDEs or editors are not smart enough?)


Conditional statement
has spaces

Copy code
The code is as follows:


if (true) {
//...
}

while (true) {
//...
}

switch (v) {
//...
}

No spaces
Copy code The code is as follows:

if(true) {
//...
}

while(true) {
/ /...
}

switch(v) {
//...
}

With spaces, 78.276%; without spaces, 21.724 %. (Based on 1,163,316 submissions.)

Single quotation mark, double quotation mark
Single quotation mark, 56.791%; double quotation mark, 43.209%. (Based on 1,705,910 submissions.)

Summary
So, the most popular code specification is:

• Comma at end of line
• Space indentation
• No space after function name
• No space between function parameters and parentheses
• Colon after object literal Space, do not add
before colon • Add space after conditional statement keyword

What is popular is not necessarily good (such as influenza), but from a communication perspective, writing code in a popular style can make your code look more familiar to most people.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template