JavaScript language essence - reading notes (4)

Appendix A malignant tumor

Click on the link for more information

Global Variables

Global variables can cause a memory leak; another large program and variable names may conflict; any part of the program can be modified (to reduce the reliability of the program) at any time

Three methods defined global variables: using external function var foo = value; global object window to add an attribute window.foo = value; undeclared variables (implicit global variables, would cause great trouble)

Scope

JS having a code block, but not block-level scope. Other languages, declare variables used in the first time, in the beginning of each function JS can declare variables in part.

Automatically inserted semicolon

If no semicolon after the return, it will automatically increase the semicolon return; return an undefined

typeof

typeof (null) => Object can not be detected by this method null

Can use null === null (true) detected null

For regular expressions, most browsers return object

parseInt

parseInt string will be converted to an integer, if you encounter a non-stop character will be resolved. parseInt ('16 tons') => 16.

If you encounter a string s first one is 0, parseInt will follow octal conversion, which may result in an error.

parseInt(‘089’) => 0

Solution parseInt ( '089', 10) => 89 for a decimal conversion.

addition

To ensure that the addition of the two numbers are integers. If the calculated required currency rounded points, we need to be calculated conversion component (reduced to element after another).

NaN

Calculation errors will produce NaN. If the calculation result is a NaN, then the entry may be calculated or generated during NaN. Analyzing isNaN may be used.

isNaN('oop'); //true
isNaN('0'); //false
Artificial arrays

JS is actually an array of objects. No need to consider the issue of cross-border, but much worse performance than the C language array. Analyzing the data can not be used typeof, it requires determination constructor.

if (my_value && typeof(my_value) === 'object' && my_value.constructor === Array) {
  console.log('parameter is an array');
}

Function parameter arguments array configuration is not a pseudo data, is an object having a length property.

hasOwnProperty is an object method, an object can be determined whether there is a key inside; however, this method may be changed (object.hasOwnProperty = null;), at this time, an error occurs.

Appendix B dross

Avoid using ==

When comparing two equal sign, if the variables will be different cast, these rules are complex. Therefore, to avoid the use of two equal sign, it is preferable to use three equal sign.

'' == '0' //false
0 == '' // true
0 == '0' // true

Avoid using the with statement

Avoid using eval statement

Avoid string transfer function parameters

Reduce the use of continue

Reduce the use of switch_case crossing (in both cases corresponds to a result, do not write a return statement.) This is not good at monitoring the effects JSLint

Make use of a code block (if while) avoid the linefeed error. Because the JS spaces are not strictly limited.

Appendix C JSlint

Use JSLint reduces errors occur (end of line code to semicolon, avoid statement error)

Use ++ - avoid writing space in front, because it may cause such an error + +

In the selection structure, JSLint do not want to appear assignment statement. Because it may be relatively statement (a == b) less an equal sign written assignment statement caused.

To avoid a potential bug caused many grammatical problems.

This is useful for themselves. In their projects the best possible library.

Appendix JSON

JSON is mainly used for data exchange in different languages.

JSON object may be inserted into any type of data values, the object can be nested unlimited levels. However, the best way is flat data. Other types of data having language objects mapped to JSON.

Eval function can use a text string to be converted into a useful data structure, but there is a risk of this method.

An alternative approach is JSON.parse contains a dangerous if the internal data, an error will run out.

Guess you like

Origin blog.csdn.net/weixin_41697143/article/details/90706309