10 Javascript design defects and javascript10 Defects

Source: Internet
Author: User

10 Javascript design defects and javascript10 Defects

I often say that Javascript is not strictly designed and has many mistakes.

The first half of today's article is about why, and the second half will list 10 design flaws in Javascript.

My reference is mainly Douglas Crockford's monograph on The essence of Javascript language (JavaScript: The Good Parts) and Fredrik Holmstr öm's article, My gripes with Javascript ). Well-developed 5-year UI front-end framework!

 

 

I. Why are Javascript design flaws?

There are three objective reasons, which leads to incomplete Javascript design.

1. the design phase is too hasty

The Javascript design took only 10 days. In addition, the designer is trying to make a difference to the company, and I am not willing to design it like this (see "Javascript birth notes").

On the other hand, the original intention of this language is to solve some simple web page interactions (such as checking whether the "user name" is filled in) without considering the needs of complex applications. The designers could not dream of writing Javascript into 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, which is probably the first in history. As of today, Javascript remains the only language in the world that uses Prototype to inherit models. This leaves no design precedent for reference.

3. Early Standardization

The development of Javascript is very fast, and there is no time to adjust the design. Well-developed 5-year UI front-end framework!

In May 1995, the design scheme was finalized. In October, the interpreter was developed successfully. In December, it was launched to the market and was immediately accepted and widely used by users all over the world. Javascript lacks a process of accumulating users from small to large, but is continuously exploding. The participation of a large number of web pages and amateur web page designers makes it difficult to adjust language specifications.

What's worse, the Javascript specification has not been adjusted yet, and it becomes solid.

In August 1996, Microsoft strongly involved, announced the release of their script language Jscript; In November, in order to suppress Microsoft, Netscape decided to apply for Javascript international standards; in June 1997, the first international standard ECMA-262 was officially promulgated.

That is to say, one and a half years after the introduction of Javascript, international standards have come into being. Design defects have not been fully exposed as a standard. In contrast, the C language was published nearly 20 years later, and international standards were issued.

2. 10 design defects of Javascript

 

 

1. Not suitable for developing large programs

Javascript has no namespace, which is difficult to modularize; there is no specification for distributing code across multiple files; repeated definitions of functions with the same name are allowed, and subsequent definitions can overwrite previous definitions, it is not conducive to modular loading.

2. Very Small standard library

The standard function library provided by Javascript is very small and can only perform some basic operations. Many functions are not available.

3. null and undefined

Null is a type of object, which means that the object is null. undefined is a data type, indicating that it is not defined.

. Code
  1. Typeof null; // object
  2. Typeof undefined; // undefined

 

The two are very confusing, but their meanings are completely different.

. Code
  1. Var foo;
  2. Alert (foo = null); // true
  3. Alert (foo = undefined); // true
  4. Alert (foo = null); // false
  5. Alert (foo = undefined); // true

 

In programming practice, null is almost useless and should not be designed at all.

4. Difficult to control global variables

Global variables of Javascript are visible in all modules. Any function can generate global variables, which greatly increases the complexity of the program. Well-developed 5-year UI front-end framework!

. Code
  1. A = 1;
  2. (Function (){
  3. B = 2;
  4. Alert ();
  5. }) (); // 1
  6. Alert (B); // 2

 

5. Automatically insert a semicolon at the end of the row

All Javascript statements must end with a semicolon. However, if you forget the extra points, the interpreter does not report an error, but automatically adds a semicolon to you. Sometimes, this will lead to some hard-to-find errors.

For example, the following function cannot achieve the expected result. The returned value is undefined instead of an object.

. Code
  1. Function (){
  2. Return
  3. {
  4. I = 1
  5. };
  6. }

 

The cause is that the interpreter automatically adds a semicolon to the return Statement.

. Code
  1. Function (){
  2. Return;
  3. {
  4. I = 1
  5. };
  6. }

 

6. plus sign Operator

+ Is used as an operator. It has two meanings: The sum of numbers and numbers, and the connection between characters.

. Code
  1. Alert (1 + 10); // 11
  2. 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.

. Code
  1. Alert (1 + "10"); // 110
  2. Alert ("10" + 1); // 101

 

This design does not need to increase the complexity of the computation. You can set another character join operator. Well-developed 5-year UI front-end framework!

7. NaN

NaN is a number that exceeds the interpreter's limit. It has some strange features:

. Code
  1. NaN = NaN; // false
  2. NaN! = NaN; // true
  3. Alert (1 + NaN); // NaN

 

Instead of designing NaN, it is better to directly report an error to the interpreter, but it is helpful to simplify the program.

8. array and object Differentiation

Because Javascript arrays also belong to objects, it is quite troublesome to distinguish whether an object is an array. The Code of Douglas Crockford is as follows:

. Code
  1. If (arr &&
  2. Typeof arr === 'object '&&
  3. Typeof arr. length = 'number '&&
  4. ! Arr. propertyIsEnumerable ('length ')){
  5. Alert ("arr is an array ");
  6. }

 

9. = and =

= Is used to determine whether two values are equal. When the two value types are different, automatic conversion will occur, and the result is not intuitive.

. Code
  1. "" = "0" // false
  2. 0 = "" // true
  3. 0 = "0" // true
  4. False = "false" // false
  5. False = "0" // true
  6. False = undefined // false
  7. False = null // false
  8. Null = undefined // true
  9. "\ T \ r \ n" = 0 // true

 

Therefore, we recommend that you use the "=" (precise judgment) Comparison character at any time.

10. basic type of packaging object

Javascript has three basic data types: String, number, and Boolean value. They all have corresponding constructors that can generate string objects, numeric objects, and Boolean objects. Well-developed 5-year UI front-end framework!

. Code
  1. New Boolean (false );
  2. New Number (1234 );
  3. New String ("Hello World ");

 

The object type corresponding to the basic data type has little effect, resulting in great confusion.

. Code
  1. Alert (typeof 1234); // number
  2. Alert (typeof new Number (1234); // object

 

For more information about Javascript, see Javascript Garden and wtfjs.com.

Iii. How do we look at the design defects of Javascript?

Since Javascript has many defects, isn't it a bad language? Is there a future?

The answer is that Javascript is not bad. On the contrary, it has powerful programming capabilities and a bright future. Well-developed 5-year UI front-end framework!

First, if good programming specifications are observed, and third-party function libraries help, most of these Javascript defects 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. Currently, many new projects have greatly extended its usage. node. js allows Javascript to be used for backend server programming, and coffeeScript allows you to write Javascript using python and ruby syntax.

Finally, these design flaws can be remedied by releasing new versions of language standards (such as ECMAscript 5. Of course, the release of standards and the implementation of standards are two different things. Many of the above defects may come along with the last day of Javascript.


What are the advantages and disadvantages of javaScript technology?

The predecessor of JavaScript is LiveScript. Since Sun launched the famous Java language, Netscape introduced Sun's Java programming concept, re-designed its original LiveScript, and renamed it JavaScript. This is called JavaScript because JavaScript is an object-based scripting language embedded in HTML documents. A large part of the syntax is similar to the Java language, and the JavaScript design makes it easy to work with the Java language, it fully supports Java applet small applications, and Java applet small applications can easily access existing JavaScript code. Therefore, JavaScript can also be regarded as a simplified version of Java.

Advantages of JavaScript

Before the emergence of a client scripting language such as JavaScript, the traditional data submission and verification work was carried out by the client browser over the network. If the data volume is large, this is an invisible waste for network and server resources. With JavaScript, you can perform data verification on the client.
JavaScript allows you to easily manipulate the objects in various browsers. You can use JavaScript to control the appearance, status, and even running mode of the browser. You can "customize" the browser based on your needs to make the webpage more friendly.
JavaScript allows multiple tasks to be completed only on the user end without the involvement of networks and servers, thus supporting distributed computation and processing.

Limitations of JavaScript

1. JavaScript simplicity

2. Explain the execution

3. Objects-based, weak type (language where data types can be ignored. It is opposite to a strongly typed definition language. A variable can assign values of different data types. The speed of a strong-type definition language may be slightly inferior to that of a weak-type definition language, but the rigor of a strong-type definition language can effectively avoid many errors .)

There are many browsers on WWW, such as Netscape Navigator, Mosaic, and HotJava. However, each browser supports different levels of JavaScript, browsers that support and do not fully support JavaScript may experience a certain difference in the effect of browsing a home page with JavaScript scripts, sometimes not even displayed.
When a JavaScript design project is set to "Web security", some functions of JavaScript need to be sacrificed. In this case, pure JavaScript cannot open, read, write, and save files on your computer. The only information that it has the right to access is the information embedded in the Web homepage. In short, JavaScript will only exist in its own small world-Web homepage.
 
Find the following 10 defects and design a test case?

A? If the distance between two elevators and the same floor is the same, how can I choose which one to run? Defect 1!
A? If one elevator is on the upper floor of a person and the other elevator is on the lower floor of a person, how can I choose? Defect 2!
B? What if the elevator on the road is fully loaded? Defect 3!
B? In the logic of B and D, if the y elevator is not idle and does not go smoothly, can the x elevator dock the singular floor above the first floor? Defect 4!
C? Similar to A, if the two elevators are in the same direction and the distance from people is the same, Which elevator is selected? Defect 5!
C? Similar to defect 2, Which elevator should I choose? Defect 6!
D? Can I perform the correct action at [,] when the elevator encounters? At this time, it cannot be connected to persons on the odd floor above the first floor. defect 7!
D? If elevator x encounters B, the result is the same as that of elevator D? Similar! Defect 8!
E? This condition is unlikely to be implemented. A, B, and C describe all the conditions, so E is not true and defect 9!

Defect 10? Not found.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.