JavaScript knowledge from eslint learn the rules

In the knowledge of the planet gradually made some eslint little knowledge, it is intended to understand the internal rules by eslint knowledge of the principles, summarized here:

prefer-const

Meaning: Always define a variable you never modified by const, if will be modified using let, never use var.

Reason: allows you to write the code more clear which variables are expected to be modified, and which are not modified. Help you sort out the logic, reducing bug

 

no-array-constructor

Meaning: Do not use the Array constructor to create an array, always use the literal.

Reason: When the Array constructor to pass a parameter can be confusing, such as new Array (3), what does it represent?

1) [undefined, undefined, undefined];

2)[3],

You might hesitate Another reason is that these two results, Array constructor may be rewritten, there are unpredictable risks.

 

no-prototype-builtins

Meaning: prohibited Object method on a prototype called directly, as foo.hasOwnProperty ( 'name') should switch to Object.prototype.hasOwnProperty.call (foo, 'name')

Reason: Object.create method allows to specify the newly created object prototype, the prototype means that the object may be ambiguous. If you create a prototype of an object with no Object.create (null), then call the Object prototype method on the error

 

func-style

Meaning: The way to specify the function definition, there are two values: expression-- expression, declaration-- function declaration

Reason: function defined by a function of the way fame, will be variable lift, it is possible to bring confusion in understanding, but the expression is not defined. To uniform the style codes, to determine the best definition of a functional style.

 

prefer-rest-params

Meaning: recommended dynamically acquire the function parameters with the remainder Syntax function f (a, b, ... args), instead of arguments

the reason:

1. The remaining parameters are obtained real array, and an array of arguments are based, and sometimes still need conversion step.

2. semantic clearer remaining parameters, i.e., the real outside the declared parameter is classified into an array of participants

 

no-useless-escape

Meaning: do not use excess escapes, official lists some of the escape character is not necessary to use the situation: "\ '";' \ " ';" \ # ";" \ e ";` \ "`; ` \ "$ {foo} \" `;` \ # {foo} `; / \ /; / \ @ /; and where required escapes:!" \ ""; ' \' '; "\ x12 ";" \ u00a9 ";" \ 371 ";" xs \ u2111 ";` \ ``; `\ $ {$ {foo}}`; `$ \ {$ {foo}}`; / \\ / g ; / \ t / g; /\w\$\*\^\./ ;

 

no-param-reassign

Meaning: Do not re-assigned to the parameters of the function. For example: function f (arg) {arg = 1;} or function f (obj) {obj.num = 1;}

the reason:

1. reassigned to the parameters of the function will change the arguments object to the back of the code risks

2. If the argument is a reference type, such as object, object attribute modification will affect the original object passed into the function

3. Impact performance V8 engine

4. change the parameters passed unwise logic itself, copy the data if necessary and then change.

 

arrow-body-style

Meaning: whether the content of the arrow function with braces wrap values have: always-- always use braces, as-needed-- required when using, never-- never use

Reason: error in order to avoid differences in syntax may bring arrow function: the function body when only one line, if not increase parentheses, by default this line of code returns the results to return. When the function body multiple lines, you must use braces, and the need to write a return statement.

 

import/no-mutable-exports

Meaning: exposure module with content export, the data should not be exposed can be changed. That export out must const defined as: const name = 'a'; export default name;

Reason: module exposure data, functions, and so should not be changed.

 

no-multi-assign

Meaning: prohibit continuous assignment, for example: let a = b = c = 1;

the reason:

1. b and c will become global variables, resulting in the risk of accidents

2. Read up is not clear, difficult to debug

 

no-case-declarations

Meaning: Do not define variables in the case of the switch statement, including the use let / const / function / class

the reason:

1. switch statement is a block-level scope, let in the case appears to be visible in the whole scope, but is in fact the implementation of the current case was only when initialized. Easy to make people confused.

2. To avoid creating multiple variables of the same name in case

Recommended wording:

1. Define the variables outside a switch statement

2. Create a new scope in single case use braces, such as: switch (name) {case 'a': {let xx = 1; break;}}

 

Bloggers welcome to join knowledge planet ~

Guess you like

Origin www.cnblogs.com/lvdabao/p/11934846.html