JavaScript explained
Explaining some dark corners of JavaScript, browsers, or Dojo for the 100th time I realized that I already did it on numerous occasions, and some of my answers are published on public web sites. So I decided to round up the most general ones I posted on StackOverflow and publish links to them here for a future reference.
JavaScript
This section is for language-specific topics. They can be equally applied in any environment.
Prototypal inheritance
A lot of people have problems with the prototypal inheritance in general and its implementation in JavaScript. These are for you guys:
- Prototype based object orientation. The good, the bad and the ugly? — a comprehensive example of implementing objects and classes with the prototypal inheritance. No libraries are used, just plain old JavaScript.
- Javascript Prototypal Inheritance Doubt II — explains how prototypes work in JavaScript, and dispels common misconceptions.
- Relation between [[Prototype]] and prototype in JavaScript — inner workings of prototypes with examples.
- JavaScript: correct prototype chain for Function — it is impossible to re-create a function object using available language tools.
- Can a JavaScript object have a prototype chain, but also be a function? — no, it cannot, and this is why.
- Why in JavaScript is a function considered both a constructor and an object? — what a constructor is with numerous simple examples, complete with fun facts.
- What is the best way to emulate “classes” in Javascript? (with or without a framework) — you asked for it: the dojo.declare() way example. Additionally it contains links to general "OOP in JavaScript" resources.
General questions
JavaScript contains some features that are puzzling even for experienced programmers coming from different languages. Some of them are clarified below:
- JavaScript Hashmap Equivalent — a super simple way to implement general hashmaps in JavaScript, or "why JavaScript doesn’t have a hash map container".
- Dynamically creating keys in javascript associative array — yes, it is very simple, and an example shows how.
- Javascript: var functionName = function() {} vs function functionName() {} — explains two distinct ways to define a function. Now you know the difference. And don’t forget to read RoBorg’s answer too — my entry corrects his answer, and adds more.
- How do I wrap a function in Javascript? — the decorator technique explained with examples.
- JS: var self = this? — another popular idiom explained with examples.
Browser
This section is for browser-specific topics. Let’s face it: many JavaScript questions have nothing to do with the language itself, but with the container of a JavaScript interpreter.
DOM
Browser’s DOM is a source of never-ending bugs, and gotchas. I tried to answer some of them:
- Javascript isDOM — How do you check if a Javascript Object is a DOM Object? — while there is no single 100% reliable method, this is my favorite technique.
- Javascript execution order issues with Dojo — positioning a DOM node is not as simple as it appears.
- Disabling button with javascript: FF vs IE — interesting quirk with forms in IE explained.
General questions
The collection of miscellaneous answers on all browser-related things:
- Recommend javascript graphics tutorials — the extensive overview of the browser-based graphics with links.
- JavaScript: Check if mouse button down? — while it sounds simple, it is far from easy to do it right. Explanations with practical examples.
- How do I reduce view duplication between client and server? — the discussion of techniques to simplify your web application’s rendering subsystem.
- Consuming remote web services from client-side script — available cross-domain request techniques to overcome the "same origin" policy.
- When to prefer JSON over XML? — my take on this popular topic.
Testing and debugging
The lack of testing and debugging tools is the first thing any newcomer to JavaScript complains about. In reality it is not that bad:
- Looking for a better Unit test tool for JavaScript — the Dojo Object Harness (DOH) unit test framework and relevant links.
- How to setup the Dojo Objective Harness test case structure for testing custom code? — yes, we can!
- Testing REST web service client when the services don’t yet exist — I use Charles (thanks to Dustin Machi who brought it to my attention).
- Debugging JavaScript in IE7 — I use VS, and this is the only good way. This short answer explains how to do it with the free version, if you cannot afford the real thing.
Dojo
Like any library Dojo has its share of misunderstandings, and underdocumented features.
Using Dojo
Hints to help you use Dojo:
- Dojo Parameters for Tabs — read Dojo sources like a pro.
- How can I pre-set arguments in JavaScript function call? (Partial Function Application) — examples on dojo.hitch(), and dojox.lang.functional tools to implement useful functional patterns.
- Is there a good embeddable code widget for blogs — hints on using dojox.highlight.
- How do you manage your DojoX code? — the overview of several techniques you can use to patch Dojo. The last technique is courtesy of Dustin Machi.
- How to make Dojo a bit more lightweight? — use CDN or a custom build — your choice.
- Struts 2 & Dojo files are too heavy and affect site’s performance.. Any remedies? — tightening up your Dojo-based web applications.
Why Dojo?
A legitimate question. Some answers:
- Which Javascript framework (jQuery vs Dojo vs … )? — why I like Dojo over other libraries.
- What JavaScript library would you choose for a new project and why? — Dojo. It is small, and it scales up well.
- Conflict between Drag and drop and sortable jquery plugins — you can go with independent plugins only that far…
- Non-web Javascript frameworks — Dojo works great in non-browser environments.