10 design flaws of Javascript
In the previous articles, I often said that the design of Javascript was not rigorous enough and there were many mistakes.
Today’s article, the first half will talk about why this is the case, and the second half will list 10 Javascript design flaws.
I refer to the literature mainly Douglas Crockford’s monograph “Javascript language essence” ( JavaScript: at The Good Parts ) and Fredrik Holmström article , “I complained to the Javascript” ( My Gripes with Javascript ).
1. Why does Javascript have design flaws?
There are three objective reasons why the design of Javascript is not perfect.
1. Too hasty in the design phase
The design of Javascript actually took only ten days. Moreover, the designer is to deal with the company, I am not willing to design this way (see “The Birth of Javascript” ).
On the other hand, the original intention of this language is to solve some simple web interactions (for example, check whether “username” is filled in), and does not consider the needs of complex applications. The designer never dreamed that Javascript could write extremely large and complex web pages like Gmail in the future.
2. No precedent
Javascript combines the features of functional programming and object-oriented programming at the same time. This is probably the first case in history. And until today, Javascript is still the only major language in the world that uses the Prototype inheritance model . This makes it have no design precedents to refer to.
3. Premature standardization
The development of Javascript is very fast, and there is no time to adjust the design.
In May 1995, the design plan was finalized; in October, the interpreter was successfully developed; in December, it was launched to the market, and it was immediately widely accepted and used by users all over the world. Javascript lacks a process of slowly accumulating users from small to large, but continuous explosive growth. The participation of a large number of established webpages and amateur webpage designers makes it difficult to adjust language specifications.
To make matters worse, Javascript’s specifications were fixed before they could be adjusted.
In August 1996, Microsoft stepped in and announced the launch of its own scripting language Jscript; in November, in order to suppress Microsoft, Netscape decided to apply for the international standard of Javascript; in June 1997, the first international standard ECMA-262 was officially promulgated .
In other words, one and a half years after the introduction of Javascript, international standards came out. Design flaws have not yet been fully exposed to become the standard. In contrast, nearly 20 years after the advent of the C language, the international standard was promulgated.
Two, 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 on how to distribute the code in multiple files; it allows repeated definitions of functions with the same name, and subsequent definitions can override previous definitions, which is not conducive to modular loading.
2. Very small standard library
The standard function library provided by Javascript is very small and can only complete some basic operations, and many functions are not available.
3. null and undefined
Null is a kind of object, which means that the object is empty; undefined is a data type, which means undefined.
typeof null; // object
typeof undefined; // undefined
The two are very easy to confuse, but the meaning is completely different.
var foo;
alert(foo == null); // true
alert(foo == undefined); // true
alert(foo === null); // false
alert(foo === undefined); // true
In programming practice, null is almost useless, and it should not be designed at all.
4. Global variables are difficult to control
Javascript’s global variables are 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); //2
5. Automatically insert a semicolon at the end of the line
All statements in Javascript must end with a semicolon. However, if you forget to add a semicolon, the interpreter will not report an error, but will automatically add a semicolon for you. Sometimes this can lead to errors that are hard to find.
For example, the following function cannot achieve the expected result at all, and the return value is not an object, but undefined.
function(){
return
{
i=1
};}
The reason is that the interpreter automatically adds a semicolon after the return statement.
function(){
return;
{
i=1
};}
6. Plus Operator
As an operator, the + sign has two meanings. It can represent the sum of a number and a number, and it can also represent a connection between a character and a character.
alert(1+10); // 11
alert(“1″+”10”); // 110
If one operation item is a character and the other operation item is a number, the number is automatically converted to a character.
alert(1+”10″); // 110
alert(“10″+1); // 101
Such a design unnecessarily aggravates the complexity of the operation, and it is completely possible to set up a character concatenated operator.
7. NaN
NaN is a number, which means that it exceeds the limit of the interpreter. It has some very strange features:
NaN === NaN; //false
NaN !== NaN; //true
alert( 1 + NaN ); // NaN
Rather than designing NaN, it is better to report errors directly by the interpreter, which is conducive to simplifying the program.
8. The distinction between arrays and objects
Since JavaScript arrays also belong to objects, it is quite troublesome to distinguish whether an object is an array or not. Douglas Crockford ‘s code looks like this:
if (arr &&
typeof arr ===’object’ &&
typeof arr.length ===’number’ &&
!arr.propertyIsEnumerable(‘length’)){alert(“arr is an array”);
}
9. == and ===
== is used to determine whether two values are equal. When the two value types are different, automatic conversion occurs, and the result is very unintuitive.
“” == “0” // false
0 == “” // true
0 == “0” // true
false == “false” // false
false == “0” // true
false == undefined // false
false == null // false
null == undefined // true
“\t\r\n” == 0 // true
Therefore, it is recommended to use the “===” (precise judgment) comparison operator at all times.
10. Basic types of packaging objects
Javascript has three basic data types: string, number, and boolean. They all have corresponding constructors, which can generate string objects, numeric objects, and Boolean value objects.
new Boolean(false);
new Number(1234);
new String(“Hello World”);
Object types corresponding to basic data types have little effect, but cause great confusion.
alert( typeof 1234); // number
alert( typeof new Number(1234)); // object
For more weird behaviors of Javascript , please see Javascript Garden and wtfjs.com .
3. How to treat the design flaws of Javascript?
Since Javascript is flawed and there are still a lot of them, is it a terrible language? Is there a future?
The answer is that Javascript is not bad, on the contrary, its programming ability is very powerful and the future is bright.
First of all, if you abide by good programming specifications, coupled with the help of third-party function libraries, most of these shortcomings of Javascript can be avoided.
Secondly, Javascript is currently the only language for web programming. As long as the Internet continues to develop, it will inevitably develop together. At present, many new projects have greatly expanded its use. Node.js enables Javascript to be used for back-end server programming, and coffeeScript enables you to write Javascript using python and ruby syntax.
Finally, as long as a new version of the language standard (such as ECMAscript 5 ) is released , these design flaws can be remedied. Of course, the release of the standard and the implementation of the standard are two different things, and many of the above-mentioned defects may remain with the last day of Javascript’s existence.
(over)