Today, let’s talk about ten javascript flaws

This article refers to “Ruan Yifeng’s network log”, worship big guy’s summary. Oh oh plus this article was written nine years ago ~~~


Why does Javascript have design flaws?

There are three objective reasons why Javascript design isn’t perfect

1. The design stage is too hasty

Javascript was designed in ten days. Also, the designer was trying to deliver to the company, which I didn’t want to do (see The Birth of Javascript). The language, on the other hand, was designed to handle simple web interactions (such as checking “username”) without considering the needs of complex applications. Designers never dreamed that Javascript would be able to create web pages as large and complex as Gmail.

3. Premature standardization

Javascript moves so fast that there is no time to tweak the design.

In May 1995, the design plan was finalized; In October, the interpreter was developed successfully. It was introduced to the market in December and was immediately widely accepted by users around the world. Javascript lacks a process of growing from small to large, slowly accumulating users, but rather a continuous, explosive proliferation of growth. The large number of established web pages and amateur web designers make it difficult to adjust the language specification. To make matters worse, the Javascript specification has not yet been adjusted and solidified. In August 1996, Microsoft intervened strongly and announced the launch of its own scripting language Jscript. In November netscape decided to apply for an international standard for Javascript in order to thwart Microsoft. In 1997, the first international standard, ECMA-262, was issued. In other words, a year and a half after Javascript came out, there was an international standard. Design flaws were standard before they were fully exposed. C, by contrast, came nearly 20 years before an international standard was issued.

10 Design Flaws of Javascript


1. Not suitable for developing large programs

Javascript has no namespace and is difficult to modularize; There is no specification for how to distribute code across multiple files; Allows for duplicate definitions of functions with the same name, with the latter overwriting the previous, making modular loading difficult.

2. Very small standard libraries

The standard library of functions provided by Javascript is very small and can only do a few basic operations, with many functions lacking.

3. The null, and undefined

Null is a type of object, meaning that the object is empty. Undefined is a data type, meaning undefined.

typeof null; / / object typeof undefined; // undefinedCopy the code

It’s very confusing, but it has very different meanings.

Var foo. alert(foo == null); // true alert(foo == undefined); // true alert(foo === null); // false alert(foo === undefined); // trueCopy the code

Null is almost useless in programming practice and should never have been designed.

4. Global variables are difficult to control

Javascript global variables, visible in all modules; Global variables can be generated inside any function, which greatly increases the complexity of the program.

A = 1; (function () {b = 2; Alert (a); }) (); / / 1 alert (b); / / 2Copy the code

5. Automatically insert semicolons at the end of lines

All Javascript statements must end with a semicolon. However, if you forget to add a semicolon, the interpreter does not report an error, but automatically adds a semicolon for you. Sometimes, this leads to errors that are hard to spot.

For example, the following function does not achieve the desired result at all. Instead of returning an object, it returns undefined.

Function (){return {I =1}; }Copy the code

6. The plus operator

As an operator, the + sign has two meanings. It can represent the sum of numbers or the concatenation of characters.

alert(1+10); / / alert (" 1 "+" 10 "); / / 110Copy the code

If one operation item is a character and the other is a number, the number is automatically converted to a character.

alert(1+"10"); 110 / / alert (" 10 + 1 "); / / 101Copy the code

Such a design unnecessarily increases the complexity of the operation, and a separate concatenation operator can be set up.

7. NaN

NaN is a number that indicates that the interpreter has exceeded its limits. It has some very strange properties:

NaN === NaN; / / false NaN! == NaN; //true alert(1 + NaN); // NaNCopy the code

Instead of designing NaN, the interpreter can simply report errors.

8. Distinction between arrays and objects

Since Javascript arrays are objects, it can be tricky to tell whether an object is an array or not. The code for Douglas Crockford looks like this:

If (arr && typeof arr === 'object' && Typeof arr.length === 'number' &&! Arr. PropertyIsEnumerable ('length')){alert("arr is an array"); }Copy the code

9. = = and = = =

== is used to determine whether two values are equal. When two value types are different, an automatic conversion occurs, and the result is very counterintuitive.

"" = =" 0 "0 = =" "/ / / / false true 0 = = = =" 0 "true/false" false "/ / false false = = = =" 0 "true/false Null == undefined // false false == undefined // true "\t\r\n" == 0 // trueCopy the code

Therefore, it is recommended to use the “===” (exact judgment) comparator at all times.

10. Wrapper objects of basic types

Javascript has three basic data types: strings, numbers, and Booleans. They all have constructors that generate string objects, number objects, and Booleans.

New Boolean (false); The new Number (1234); new String("Hello World");Copy the code

The object types that correspond to the base data types are of little use but of great confusion.

alert( typeof 1234); // number alert(Typeof New Number(1234)); // objectCopy the code

For more Javascript quirks, see Javascript Garden and wtfjs.com.

How do you view the design flaws of Javascript?

Given Javascript’s flaws and its abundance, is it a bad language? Is there a future?

The answer is that Javascript isn’t bad, but rather powerful and promising.

First, most of these pitfalls of Javascript can be avoided if good programming practices are followed, with the help of third-party libraries.

Second, Javascript is currently the only language for web programming, and as long as the web continues to evolve, so will it. Node.js makes Javascript available for back-end server programming, and coffeeScript lets you write Javascript using Python and Ruby syntax.

Finally, these design flaws can be remedied by simply releasing a new version of the language standard, such as ECMAscript 5. Of course, publishing a standard and implementing a standard are two different things, and many of the flaws mentioned above will probably remain with Javascript until the last day of its existence.