Javascript is built on some very good ideas and a few very bad ideas. This requires JS programmers to learn to take its essence, avoid its dregs. In order to improve the quality and readability of JS code, avoid some potential dangers.

Here I mainly talk about some of the bad features of JS, and how to avoid it.

Js bad features

The global variable

  • Because js was only used as a language for writing small scripts in the early days, it was not expected to be used as a front-end mainstream language development trend. Different module JS cannot exist in the same file, let alone be packaged as a project. So in the pursuit of a faster and more convenient way invented JS early global variable dependence is quite serious.
  • Problem: The most serious problem with global variables is that they are at the top of the scope chain. All developers, regardless of location, can get the variable and read and write to it. And when a project is large, it’s extremely easy for one developer to declare a variable and the next developer to overwrite it by declaring the same variable. Both phenomena are prone to great dangers.
  • Solution: At present, the mainstream is the introduction of modular development, to avoid variable naming conflicts. Es6 block level declaration scheme is used in the writing process, and some existing global variables should not be modified as far as possible.
  • Summary: With the popularity of ES6, CMD, AMD, import/export. The above phenomenon may be rare, but these solutions, are the previous foot a pit, we do not need to step on these pits.

Automatic insertion of semicolons

  • Js exists as a scripting language. Therefore, for compressed JS code, there is a mechanism that will try to insert semicolons automatically to correct defective programs. This feature may be a boon to programmers who prefer short code, but this mechanism is prone to bigger errors.
  • Phenomenon:
if(condition) 
    actuating logic1
    actuating logic1
Copy the code
    • This code may indeed be short. But this is a poor way to write, both for readability and for code specification.
  • Solution: In the actual writing process, for all easy to generate ambiguity code phenomenon to develop the habit of cleanliness.

Unicode

  • At the time of the JavaScript language, there was no UTF-16 encoding, so ucS-2 was the only encoding scheme. Causes all characters to be 2 bytes in this language, and 4 byte characters are treated as two double-byte characters.
  • Phenomenon: with charactersFor example, its UTF-16 encoding is the 4-byte 0xD834 DF06. The problem is that the 4-byte encoding is not part of UCS-2, and JavaScript does not recognize it, just as two separate characters U+D834 and U+DF06. As mentioned earlier, these two code points are empty, so JavaScript will assume thatIs a string of two null characters!
  • Solution: In the ES5 era and before, only programmers could write their own functions to judge and filter characters. After ES6, targeted solutions were carried out to solve this problem. But it’s worth being aware of some of the es5-related code.

typeof

  • Source: In the original version of JS, a 32-bit system was used. To improve performance, low values are used to store variable type information. Save on memory usage.
    • The type-id relationship is as follows
The data type Machine code identification
Object 000
int 1
float 010
String 100
Boolean 110
undefined -2^31(binary all 1)
null Full of 0
  • The result of typeof NULL is Object. However, null instanceof Object results in false.
  • Solution: The problem is rooted in history. The reader of the prototype chain will notice that the top of the prototype chain is NULL. Object. Prototype; null; Object. Can be used according to the principle of prototype chain instanceOf, or use of the Object. The prototype. ToString. Call method is used to identify the run.

Floating point Numbers

  • The floating point standard adopts the binary floating point arithmetic standard (IEEE 754). All floating-point operations are converted to binary before they are performed, so overflow is possible.
  • Phenomenon: 0.1 + 0.2! = = 0.3.
  • The solution: Convert a floating point number to an integer, then convert it back. Or use an arithmetic precision library like bignumber.js.

An array of

  • The inheritance and extension mechanism adopted by JS is the prototype chain mechanism, so everything in JS starts from the copy of Object. Js arrays are not real arrays compared to arrays in other languages. It’s just a higher-order object like a list. It is just a different constructor than an object, and its values are arranged in order.
  • For of can iterate over a set of values as well as an object value.
  • Solution: Considering the different characteristics of arrays and objects, think about the scenarios they need to deal with, and give full play to the advantages of arrays and objects.

summary

  • Js itself due to its original creation of time and historical positioning will inevitably produce some historical legacy problems, some even once let JS developers quite distressed. But in programming, there are always more ways than difficult, so there are mature solutions or alternatives to some of the most common historical problems. But that doesn’t mean we can just use it without knowing the historical reason for its existence, which is unprogrammer style.
  • Knowing the other side of JS, we should not forget the excellent features of JS. Js can go from rough to one of the mainstream languages today, these excellent features play a certain decisive role. The summary is as follows:
    • Function: Early JS design absorbed the function first-class citizen design of Scheme and programmed functions as first-class citizens. This is quite developer-friendly. (Details can be viewed, I will not elaborate)
    • Prototype chain mechanism: JS inheritance uses Self’s prototype chain mechanism.
    • Literal representation: Compared with other languages, JS directly represents data structures. Objects and data structures can be created through a series of literals. That’s one of the reasons for JSON.

The last

All great actions and thoughts, all have a insignificant beginning