Home > Web Front-end > JS Tutorial > body text

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

php是最好的语言
Release: 2018-07-30 09:50:15
Original
2304 people have browsed it

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: $("

")

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 tag  * @return 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'

  • ' 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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!