Why is there a design flaw in JavaScript?
There are three objective reasons, which lead to a lack of complete javascript design.
1. The design phase is too hasty
JavaScript design, in fact, only took 10 days. Moreover, the designer is in order to the company, I actually do not want to design (see the birth of JavaScript).
On the other hand, the language was designed to solve some simple web interactions (for example, checking whether the "username" was filled out), and did not consider the need for complex applications. Designers never dreamed that JavaScript would be able to write a very large and complex web page like Gmail in the future.
2. No precedent
JavaScript combines both functional programming and object-oriented programming, which is probably the first example of history. And until today, JavaScript remains the only major language in the world that uses the prototype inheritance model. This makes it not possible to design precedents for reference.
3. Premature standardization
JavaScript is developing very quickly and there is no time to adjust the design at all.
In May 1995, the design was finalized; in October, the interpreter was developed successfully; December, to market, immediately widely accepted, the world's users are heavily used. JavaScript lacks a process of small-to-large, slowly accumulating users, but a continuous explosion of proliferation growth. The involvement of a large number of both web and amateur web designers makes it difficult to adjust language specifications.
To make things worse, JavaScript's specs haven't come and been adjusted, and they're cured.
In August 1996, Microsoft's strong involvement, announced its own scripting language jscript;11 months, in order to suppress Microsoft, Netscape decided to apply for JavaScript international standards, June 1997, the first International standard ECMA-262 formally promulgated.
In other words, when JavaScript launches 1.5, international standards come into existence. Design flaws have not yet been sufficiently exposed to become standard. By contrast, international standards were enacted almost 20 years after the advent of the C language.
Ii. 10 design flaws in JavaScript
1. Not suitable for developing large programs
JavaScript does not have namespaces (namespace), it is difficult to modularize, there is no specification for how to distribute code across multiple files, a duplicate definition of a function with the same name is allowed, and subsequent definitions can overwrite the previous definition, which is not conducive to modular loading.
2. Very small standard library
JavaScript provides a very small library of standard functions, only a few basic operations, and many features are not available.
3. Null and undefined
Null belongs to one of the objects (object), meaning that the object is empty; Undefined is a data type, which means undefined.
typeof null; Object
typeof undefined; Undefined
The two are very confusing, but the meanings are 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 should not be designed at all.
4. Global variables are difficult to control
JavaScript's global variables are visible in all modules, and 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. Automatic insertion of end-of-line semicolon
All the statements of JavaScript must end with a semicolon. However, if you forget to add a semicolon, the interpreter does not give an error, but instead automatically adds a semicolon to you. Sometimes, this can lead to some hard-to-find errors.
For example, the following function does not achieve the desired result at all, the return value is not an object, but a 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
The + sign, as an operator, has two meanings, can represent numbers and numbers, and can also represent the connection of characters to characters.
alert (1+10); 11
Alert ("1" + "10"); 110
1
2
alert (1+10); 11
Alert ("1" + "10"); 110
If one action item is a character and the other action item is a number, the number is automatically converted to a character.
Alert (1+ "10"); 110
Alert ("10" + 1); 101
Such a design unnecessarily exacerbates the complexity of the operation, and it is entirely possible to set up a separate operator for a character connection.
7. NaN
Nan is a number that exceeds the limits of the interpreter. It has some very strange features:
Nan = = = Nan; False
Nan!== nan; True
Alert (1 + NaN); NaN
Rather than the design of Nan, the interpreter directly error, but it helps to simplify the program.
8. The distinction between arrays and objects
Since JavaScript arrays also belong to Objects (object), it is quite cumbersome to distinguish whether an object is an array or not. The code for Douglas Crockford is 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 conversions occur, and the resulting results are very counterintuitive.
"" = = "0" False
0 = = ""//True
0 = = "0"//True
False = = "false"//False
false = = "0"//True
false = = undefined//False
false = = NULL//False
NULL = = undefined//True
"TRN" = = 0//True
Therefore, it is recommended to use the "= = =" (exact) comparator at any time.
10. Basic types of Packaging objects
JavaScript has three basic data types: string, numeric, and Boolean. They all have corresponding constructors that can generate string objects, numeric objects, and Boolean objects.
Javascript
New Boolean (FALSE);
New number (1234);
New String ("Hello World");
The type of object that corresponds to the base data type is very small, and the confusion is large.
Alert (typeof 1234); Number
Alert (typeof new Number (1234)); Object
For more bizarre behavior on JavaScript, see JavaScript Garden and wtfjs.com.
Third, how to look at the design flaws of JavaScript?
Since JavaScript is flawed and has a lot of numbers, is it a bad language? Is there a future?
The answer is that JavaScript is not bad, but it has great programming power and a bright future.
First of all, these flaws in JavaScript can be largely circumvented if good programming practices are followed, along with the help of third-party libraries.
Second, JavaScript is currently the only language for Web programming, and as long as the internet continues to evolve, it will inevitably evolve together. Currently, many new projects have greatly expanded its use, and node. js makes JavaScript available for back-end server programming, and Coffeescript allows you to write JavaScript in Python and Ruby syntax.
Finally, these design flaws can be remedied by releasing a new version of the language standard (such as ECMAscript 5). Of course, standard releases and standard implementations are two different things, and many of these flaws may be accompanied by the last day of JavaScript's existence.
Nanyi's Views on JS (10 major flaws)