10 design flaws of JavaScript

  Peter        2012-11-29 11:39:35       18,486        4         

JavaScript's design took only ten days. Moreover, the designer didn't want to design it initially, he just wanted to complete the task assigned by company. It is now a very powerful client side programming language used in almost all the websites. It's an excellent language, but it also has some flaws.

1. Not suitable for large projects

JavaScript doesn't have namespace, it's hard to be modular, there is no standard for putting codes in multiple source files. It allows defining functions with the same name, the function defined later will override the one defined previously, it's hard for method overloading.

2. Small standard library

JavaScript provides small standard function library.

3. null and undefined

null is one kind of object, it means the object is null; undefined is a data type, it means something undefined.

  typeof null; // object
  typeof undefined; // undefined

It's very easy to mess up these two, but the meaning is completely different.

  var foo;
  alert(foo == null); // true
  alert(foo == undefined); // true
  alert(foo === null); // false
  alert(foo === undefined); // true

4. Global variable

JavaScript's global variables are visible in all modules, we can create global variable inside any function. This increases the complexity of program.

  a = 1;
  (function(){
    b=2;
    alert(a);
  })(); // 1
  alert(b); //2

5. The insertion of semi-colon at line end

Every statement in JavaScript should be ended with semi-colon. But if you forget to add semi-colon at the line end, the interpreter will not output the error, it will add the semi-colon at line end. Sometimes it may cause some errors difficult to find.

For example, the following function will not produce the expected result, the returned value is not an object, but undefined.

  function(){
    return
      {
        i=1
      };
  }

 

The reason is the interpreter will add a semi-colon at the end of return.

  function(){
    return;
      {
        i=1
      };
  }

6. + operator

+ as an operator has two meanings : can be used to add two numbers up or concatenate two strings.

  alert(1+10); // 11
  alert("1"+"10"); // 110

If one operand is string, the other operand is number, then the number will be converted to string automatically.

  alert(1+"10"); // 110
  alert("10"+1); // 101

This design increases the complexity of calculation.

7. NaN

NaN is a number, it means it overflows, it has some strange features:

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

8. The difference between array and object

Since JavaScript's array is also object, so if we want to find whether an object is an array, it's very difficult. Douglas Crockford provides the following code.

  if ( arr && 
    typeof arr === 'object' &&
    typeof arr.length === 'number' &&
    !arr.propertyIsEnumerable('length')){
    alert("arr is an array");
  }

9. == and ===

When two values are different, it will auto cast.

  "" == "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

So we recommend to use === whenever possible

10. The encapsulated object of primitive types

JavaScript has three primitive data types: string, number and boolean. They have respective value and function, it will create string object, number object and boolean object.

  new Boolean(false);
  new Number(1234);
  new String("Hello World");

Some mess up

  alert( typeof 1234); // number
  alert( typeof new Number(1234)); // object

Author : coffeescript Source : http://blog.csdn.net/coffeescript/article/details/8212541

JAVASCRIPT  DESIGN FLAW  OBJECT 

       

  RELATED


  4 COMMENTS


Darkyen [Reply]@ 2013-05-09 07:07:49

Hard time learning javascript, are we ? 

Darkyen [Reply]@ 2013-06-15 23:29:11

null is not an object, as typeof incorrectly suggests. For instance, (null instanceof Object) === false. And according to the ECMAScript spec, both null and undefined are primitive data types.

GuncPinue [Reply]@ 2013-06-18 14:56:35
You are mistaken. I can prove it. Write to me in PM, we will discuss.
GuncPinue [Reply]@ 2013-06-25 06:26:22
I think, that you commit an error. I can defend the position. Write to me in PM.


  RANDOM FUN

How Android will ruin a day