Rule 1.2 Use abbreviations as few as possible, except for common words or professional words. For example, ``context`` can be shortened to ``ctx``, ``request`` can be shortened to ``req``, and ``response`` can be shortened to ``resp``.
Recommendation 1.6 Do not use negative Boolean variable names. Local variables or methods of the Boolean type must be prefixed with expressions with the meaning of right or wrong.
**Note:** There are three methods for defining a function: function
declaration, function constructor, and function expression. Regardless
of in which method you define a function, they are instances of the
Function object and inherit all default or custom methods and properties
of the Function object. The method of creating a function using a
function constructor is similar to the character string ``eval()``. Any
character string can be used as the function body, which may cause
security vulnerabilities.
**Counterexample:**
.. code:: javascript
var func = new Function('a', 'b', 'return a + b');
var func2 = new Function('alert("Hello")');
**Example:**
.. code:: javascript
function func(a, b) {
return a + b;
}
var func2 = function(a, b) {
return a + b;
}
Suggestion 3.11 When using the prototype \`prototype’ to implement inheritance, try to use the existing stable library methods instead of self-implementing them.
**Note:** JavaScript has a single numeric type: ``IEEE 754``
double-precision floating point number. Having a single numeric type is
one of the best features of JavaScript. Multiple number types can be a
source of complexity, confusion and error. However, one of the biggest
drawbacks of the binary floating-point type is that it does not
accurately represent the fractional part, causing unexpected precision
problems, as shown in the following examples.
Sample Code1:
.. code:: javascript
console.log(0.1 + 0.2 === 0.3); // Output: false. Therefore, do not use == or === to compare floating-point numbers.
Sample Code2:
.. code:: javascript
var sum1 = (0.1 + 0.2) + 0.3;
console.log(sum1); // Output: 0.6000000000000001
var sum2 = 0.1 + (0.2 + 0.3);
console.log(sum2); // Output: 0.6. Therefore, for binary floating-point numbers, (a + b) + c cannot be guaranteed to produce the same result as a + (b + c).
The effective solutions are as follows:
1. Use integers as much as possible because integers do not need to be
rounded.
2. The native JavaScript method
``Number.prototype.toFixed(digits)``,\ ``digist`` is used to indicate
the number of digits after the decimal point. The exponential method
is not used. If necessary, the number is rounded off. This method is
used to reduce the precision of the calculation result before
determining the floating-point number calculation result. The sample
code is as follows:
.. code:: javascript
parseFloat(0.1 + 0.2).toFixed(1); //0.3
3. A very small constant ``Number.EPSILON =.220446049250313e-16`` is
added to ES6, which is about 0.00000000000000022204.
``Number.EPSILON`` is used to determine the calculation error of
floating-point numbers. If the calculation error of floating-point
numbers is less than or equal to the value of ``Number.EPSILON``,
such an error is acceptable. The sample code is as follows: