Garo Garabedyan's Divergent Thinking Blog

Crockford: JavaScript’s bad and good parts

with 2 comments

Douglas Crockford, a programmer involved in the ongoing development of JavaScript, popularizing the JSON (JavaScript Object Notation) data format and currently a senior JavaScript architect at Yahoo!, have wrote a book and gave talks about the good and bad parts in JavaScript proving that most of the professional developers using JavaScript are not aware of the beauty of a programming language hosted on more virtual machines than anything else on the planet—on every fully-capable web browser. With a series of interesting examples Crockford outlines the most common problems in developing JavaScript applications, provides workarounds and outlines the good practices.

ECMAScript is widely used for scripts executed on the client (Internet applications separated between the server and the client) in the form of well known dialects like JavaScript, JScript and ActionScript. There is a project to the ECMAScript’s dialect JavaScript as a server-side engine (Node.js).

JavaScript is criticised by advanced developers in other programming languages for its weak type system and lack of raising proper exceptions, but it is irrefutable that there is no such programming language in the world like JavaScript, which to be used by such a wide range of users- starting with copy-and-pasters to professional software developers using JavaScript for scientific purposes.

ECMAScript supports object-oriented programming. Objects in ECMAScript are values with named properties. Object properties that are functions can be called as methods. ECMAScript functions are objects and can be stored as properties, passed as arguments, and returned as results. This powerful idiom from functional programming allows your functions and methods to import functionality from their caller in a simple and flexible way. ECMAScript objects inherit properties from prototype objects. Prototype-based programming facilitates easy delegation and flexible overriding of object behavior. [About ECMAScript. http://www.ecmascript.org/about.php]

Crockford’s experience reveals that JavaScript is a language that people use without bothering to learn it first.

JavaScript’s Bad Parts

  • Global variables: Due to the lack of a linker in JavaScript, compiling took place in a global namespace where any variable can collide and interfere with each other. XSS attacks are fundamentally enabled by JavaScript’s use of global variables.
  • Operator + adds and concatenates: This overloading in a type-unsafe programming environment causes problems.
  • Semicolon insertions: JavaScript tries to put semicolons instead of the programmer in order to make the C syntax easier for beginners. When the compiler gets an error, it goes back, looks for a line feed and replaces it with a semicolon.
  • Operator typeof: JavaScript typeof operator returns for the type of an array object and for the type of null object.
  • With statement: slows the execution too much.
  • Eval function: mostly misused function in JavaScript.
  • Phony arrays: While in most languages arrays are linear sequences of memory divided into regularly spaced buckets where easily an address can be computed to the element in the array, in JavaScript arrays are essentially hash tables in which the keys are turned into strings. Due to this behavior arrays have a terrible performance in JavaScript, but makes the programming model easier as an array’s dimension is not set.
  • == and != do type coercion.
  • Too many bottom values like false, null, undefined, NaN.

Consequences of type coercion of the double-equal operator (very strange and ruled by a complicated rules- http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262%20edition%205.1,%20June%202011.pdf Section 11.9.3  The Abstract Equality Comparison Algorithm):

'' == '0' //false
0 == '' //true
0 == '0' //true
false == 'false' //false
false == '0' //false
false == undefined //false
false == null //false
null == undefined //true
" \t\r\n " == 0 //true

JavaScript has a triple-equal operator which checks types in making a comparison. The triple-equal operator is highly recommended.

The for in operator is troublesome as it makes a deep dredge on the all members of the object.

Blockless statements, expression statements, floating point arithmetic, ++ and — operators, switch operator are a bad heritage. Switch statement is replaced by advanced JavaScript developers with an object encapsulating the possible cases of the switch block (http://www.sitepoint.com/google-closure-how-not-to-write-javascript/).

JavaScript’s Good Parts

  • Lambda.
  • Dynamic objects: At any time a property can be added to an object. Developer does not need to add the property to a class. Dynamic objects turned out to be amazingly powerful. A form of reflection occurs in JavaScript when you ask an object about the value of a property which it has not-JavaScript returns undefined.
  • Loose Typing.
  • Object Literals: a very nice notation for describing objects. JavaScript’s object literals were the inspiration for the JSON data interchange format.

Inheritance in JavaScript

Inheritance is object-oriented code reuse. There are two schools: classical and prototypal. Prototypal inheritance is class-free where objects inherit from objects. In JavaScript an inherited object contains a link (named __proto__) to the parent object. This inheritance is called Delegation or Differential Inheritance.

Below is a pattern of object creation with inheritance in JavaScript:

function myPowerConstructor(x) {
    var that = otherMaker(x); /* create an object by Object literal,
                                 or with new keyword,
                                 or Object.create,
                                 or call another power constructor */
    var secret = f(x);
    that.priv = function () {
        ... secret x that ...
    };
    return that;
}

Variable called “that” (without the quotes) is used to contain the new object. Please note that “this” (without the quotes) is a reserved name. The variable “secret” (without the quotes) is private. Method called “priv” (without the quotes) is a closure and, thus, has access to all private variables and can be invoked by the outside world. Closures are functions referencing the environment in which they were created (their context).

Right-curlies (“block {” all on the same line) is the only acceptable style in JavaScript. Due to the insertion of semicolon on every line returning an error a return clause must be followed on the same line with a right-curlie or a value.

JSLint

JSLint is a code quality analyzer for JavaScript code freely available at http://www.jslint.com/ . JSLint defines a professional subset of JavaScript. It imposes a programming discipline that makes developers much more confident in a dynamic, loosely-typed environment.

Advertisements

Written by garabedyan

July 5, 2011 at 20:33

2 Responses

Subscribe to comments with RSS.

  1. We’re a group of volunteers and starting a new scheme in our community. Your web site offered us with valuable info to work on. You’ve done an impressive job and our entire community will be thankful to you.

    Marsha Desilvo

    January 23, 2012 at 11:03

  2. Perfectly written subject material , appreciate it for information .

    tchat rencontre

    January 27, 2012 at 00:41


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s