Good JS coding habits and style

First started on fxm5547's blog

format

No need to add after each sentence js; assignment=Add a space before and after the number Example:

const RECEIVE_ZEN = 'RECEIVE_ZEN'
const REQUEST_ZEN = 'REQUEST_ZEN'
const CLEAR_ZEN = 'CLEAR_ZEN'
Copy code

Use Babel to write ES6/7 code

ES6/7 has added a lot of new features, so that we can better write javascript BabelWe can write ES6/7 and finally compile to ES5 to ensure that there are no compatibility issues.

Minimize the use of jQuery

jQuery has slowly faded out of our sights youmightnotneedjquery You-Dont-Need-jQuery

Use functional programming

JS Functional Programming Guide Javascript functional programming style

Using the ES6 module mechanism

Introducing import foo from 'module' export

const foo = {
  ...
}
export default foo
Copy code

Defer and async

<script src="script.js"></script> Without defer or async, the browser will immediately load and execute the specified script, "immediately" means before rendering the document element under the script tag, that is, without waiting for the follow-up The loaded document element is loaded and executed when it is read.

<script async src="script.js"></script> With async, the process of loading and rendering subsequent document elements will be done in parallel (asynchronous) with the loading and execution of script.js.

<script defer src="script.js"></script> With defer, the process of loading subsequent document elements will be done in parallel with the loading of script.js (asynchronous), but the execution of script.js will be done after all elements have been parsed, before the DOMContentLoaded event fires. carry out.

The difference between the two is when the script is executed after it is downloaded. Obviously defer is the closest we have to the application script loading and execution requirements.

The key point about defer is that it executes the script in the order of loading, which is a good use of this.

Async is a master of out-of-order execution. Anyway, the loading and execution of the script is tight, so no matter what order you declare, as soon as it is loaded, it will execute immediately.

Think about it, async is not very useful for application scripts because it doesn't consider dependencies at all (even at the lowest level of execution), but it's for scripts that don't depend on any scripts or are not dependent on any scripts. Very suitable, the most typical example: Google Analytics

So most of the places where we load scripts can be replaced with defer.

Intelligent Recommendation

Good HTML coding style

First started on fxm5547's blog HTML coding specification 1 Introduction As a hypertext markup language that describes the structure of web pages, HTML has been widely used in Baidu. The goal of this ...

HTML good coding style

First started on fxm5547's blog HTML coding specification 1 Introduction As a hypertext markup language that describes the structure of web pages, HTML has been widely used in Baidu. The goal of this ...

Good coding style of if else?

Good coding style of if else? About code indentation style, have their say. To if / else, for example, at least two kinds of style. 1.       2.   The first personal bias, there are...

Python's good coding style

Coding Style For Python,PEP 8 Most of the projects has become a style guide followed; it promotes a very readable and pleasing coding style. Every Python developer should read it sometime; the followi...

Java coding style: space usage habits

Coding style: space usage habits What is the personal coding style? An example is as follows: Test0523.java What is the personal coding style? When I read the book, I suddenly found out that there wer...

More Recommendation

30 Java coding specifications! Develop good coding habits!

(1) The first letter of the class name should be capitalized. The first letter of a field, method, and object (handle) should be lowercase. For all identifiers, all the words contained should be close...

ArcGIS API for JS general coding habits

1, reference claro.css and esri.css 2, Page Layout 3, page styles 4, page load response process     ...

Java good coding style (2)

(3) OOP Statute [Forced] Avoid accessing static variables or static methods of this class through object references of a class. It is unnecessary to increase the compiler parsing cost and access it di...

Chapter 4: js programming principles and good habits

table of Contents Reserved retreat Separate javaScript Backward compatibility 1. Reserved retreat In the case that the user website does not support js, you can visit your website smoothly, that is, t...

Coding habits

Judge sentences The if statement determines whether the variable is equal to an immediate value, as follows: The above writing method may sometimes write "==" as "=", the compiler ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top