Table of Contents
1. When using quotation marks, single quotation marks ' ' take precedence (if it is not nested quotation marks, do not use double quotation marks)
2. Issues with the use of spaces: (after the keyword symbol, after the typesetting function assignment symbol = ), etc.
3. Problems with the same line but different lines :
6. It is customary to add window to the properties and methods of window. For some exceptions, you do not need to add window: document. console, navigator. For example: window.alert('hi')
7. The same concatenation method is very long and requires line breaks and indentation. The ternary operator in js, concatenation in jq, etc.
 var location = env.development ? 'localhost' : 'www.api.com' Written in one line
  var location = env.development
  console.log(value)
10. Problems with creating objects and arrays: var item = {}; without using the new Object() method. Arrays: var arr = []
11. Problems with connecting strings of more than 80 characters:
12. Use parseInt for numbers and always bring the base for type conversion. var val = parseInt(inputValue, 10);
13. Use for Boolean value conversion Boolean() or!! var hasAge = Boolean(age); var hasAge = !!age;
14. Naming problem:
Home Web Front-end JS Tutorial Practical, a compilation of ES6 code specification writing methods in js development

Practical, a compilation of ES6 code specification writing methods in js development

Jul 30, 2018 am 09:50 AM

ES6 code standard writing is organized, the use of quotation marks, single quotation mark ' ' takes precedence (if it is not a nested quotation mark, do not use double quotation marks), the use of spaces: (after the keyword symbol typesetting function assignment symbol =), etc. .

1. When using quotation marks, single quotation marks ' ' take precedence (if it is not nested quotation marks, do not use double quotation marks)

Normal situation: console.log('hello there') Double quotation marks Code: $("<p class='box'>")

2. Issues with the use of spaces: (after the keyword symbol, after the typesetting function assignment symbol = ), etc.

  a Function brackets: function hello (name) {} Look at (parameters) The "left and right outside the brackets" ( ) have spaces, and the "left and right name inside the brackets" have no spaces

b A space is required after the keyword: if (condition) { ... } A space is required between if and ()

c Assignment symbol = Spaces are required on both sides: var x = 2 Assignment symbol = Spaces are required on both sides

d Spaces are required on both sides of the string splicing symbol: var message = 'hello, ' name '!' The number between constants and variables, spaces are required on the left and right sides

e Comma, do not leave it in front Space, leave a space after: var list = [1, 2, 3, 4] function greet (name, options) { ... } Do not leave a space before the comma

3. Problems with the same line but different lines :

If () {} else {}: } else { To be in one line

                                                                                                                         } else {

         //

      }

4. Do not write unused variables. If a variable is defined and has not been used in operations, it should not be defined. this variable.

5. Use === instead of ==. When comparing equal, == will take one more step of data conversion, but when in the if (a!=undefiend) {} ​​condition, a!=undefiend simultaneously There are dual meanings of a!==undefiend and a!==null (null == undefined)

6. It is customary to add window to the properties and methods of window. For some exceptions, you do not need to add window: document. console, navigator. For example: window.alert('hi')

7. The same concatenation method is very long and requires line breaks and indentation. The ternary operator in js, concatenation in jq, etc.

 var location = env.development ? 'localhost' : 'www.api.com' Written in one line

  var location = env.development

    ? 'localhost'

   : 'www .api.com'

Continuous writing:

var leds = stage.selectAll('.led')  .data(data) . enter().append('svg:svg')

  .class('led', true)

  .attr('width', (radius margin) * 2)
  .append('svg: g')
 .attr('transform', 'translate(' (radius margin) ',' (radius margin) ')')
  .call(tron.led);

8 , Comment question: There should be a blank line with the previous line. In addition, there should be no large blank lines for no reason.                                                                                                                                                                                                                                                                               Empty line

   / / Here are the comments

  console.log(value)

 

Multi-line comments

: (

This can also be used for copyright information comments

 /**

  * make() returns a new element

  * based on the passed in tag name  *  * @param <String> tag  * @return <Element> element  */


9. Problem at the beginning: Do not start with ( [ ` , but add the ; sign
# before the beginning.
##  ;(function () {window.alert('ok')}())

   ;[1, 2, 3].forEach(bar)   

  ;` hello`.indexOf('o')

10. Problems with creating objects and arrays: var item = {}; without using the new Object() method. Arrays: var arr = []

11. Problems with connecting strings of more than 80 characters:

 var errorMessage = 'This is a super long error that '

'was thrown because of Batman.'

'When you stop to think about '

'how Batman had anything to do '

'with this, you would get nowhere '

'fast.';

Use the join method for loops or multi-line strings To construct

function inbox(messages) {

items = [];

for(i = 0; i < length; i ) {

Items[i] = messages[i].message;

 }

return'<ul><li>' items.join() ;

}

12. Use parseInt for numbers and always bring the base for type conversion. var val = parseInt(inputValue, 10);

13. Use for Boolean value conversion Boolean() or!! var hasAge = Boolean(age); var hasAge = !!age;

14. Naming problem:

a When naming private properties, add an underscore_ in front of it, such as: In the constructor, this._firstName = 'Panda'; var _firstName = firstName;

b Add a $ to the name of the jq variable to distinguish the js variable

Related articles:

Introduction and export of commonJS and es6 specifications

Daily js development specifications

Related videos:

Javascript - ES6 practical video course-free online video tutorial

The above is the detailed content of Practical, a compilation of ES6 code specification writing methods in js development. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

10 Mobile Cheat Sheets for Mobile Development

See all articles