What I like and don’t like about JavaScript
Like:
- Loose Typing. It has benefits, much greater expressive power, often programs tend to be easier to write.
- Objects as general containers. Brilliant idea, unification of objects and hash tables, objects are completely dynamic things in which new members and new methods could be added at any time.
- Objects can be augmented. You can take an existing object and add new stuff to it at any time — new members, new methods. And we do it simply be assignment. We don’t need to make a new class (in fact there are no classes).
- One of the really powerful things in JavaScript is that we can extend the built-in types. We can extend all objects at once by assigning to Object.prototype or we can enhance all arrays by assigning to Array.prototype.
- Prototypal inheritance. Really distinct from classical inheritance, objects inherit directly from other objects, there are no classes, powerful idea, not commonly understood very well.
- Lambda. The next powerful idea — the use of functions as first class objects. Functions can be passed, returned and stored just like any other value — this turns out to be really-really powerful and important. What JavaScript calls function, other languages call lambda. It is a source of enormous expressive power.
- Inner functions. Functions do not have to be defined at the top level. Functions can be defined inside of other functions. That turns out to be a really useful thing.
- Static Scoping or Lexical Scoping. An inner function has access to the variables and parameters of functions that it is contained within.
- Closure. The scope that an inner function enjoys continues even after the parent function have returned. The #1 really powerful feature in JavaScript.
Don’t like:
- Linkage through global variables. Because of the load and go model, JavaScript doesn’t have any sort of linker, so separate compilation units are all combined in a common global namespace, which turns out to be a really bad idea. It has security problems, reliability problems.
- Implied global variables. Any var which is not properly declared is assumed to be global by default.
- Only functions have scope, blocks do not have scope. This causes people to think incorrectly about variable scope. If you declare a variable any place inside of a function, it’s visible everywhere in that function.
- When a function is called in the function form, [this] is set to the global object. That is not very useful. It makes harder to write helper functions within a method because the helper function does not get access tho the outer [this]. var that = this.
- Confusing implementation of prototypal inheritance, the language itself is confused about its prototypal nature.
- typeof array = object, which isn’t wrong, but it’s not very helpful.
- Name “JavaScript”. Confusing.