As a regular Java (and occasionally other languages) developer, I've sometimes used javascript with this constant sense of weirdness on the edges, and without being able to grasp the concepts in it.
In fact, the browsers' DOM and the history of the Javascript implementations in browsers contributed to this fuzziness (as we'll see later).
Basic concepts:
Javascript has very little to do with Java. In fact its name only contains java for marketting purposes of that time. Javascript is a language from the C family like java and uses a lot of the same operators and syntax.
This can be confusing, because Java and Javascript are indeed very different.
While Java is based on classes and inheritance between classes, Javascript is based on objects and inheritance between objects.
Javascript is also a prototype-based language (which is I think the hardest part to grasp...).
This may sound a bit abstract, but one cannot hope coding proper javascript without at least understanding these concepts.
A little pause
Before we start explaining the previous concepts, you must be aware that Javascript's history has been very clumsy to say the least.
Some good decisions were taken, and some very bad ones.
Most of the bad decisions were made during the browsers' war (Netscape vs Internet Explorer) with a purpose to attract developers from Java/C++ or VBScript for example.
These bad decisions leave strange reserved words in the language (like 'abstract' or 'interface' which are never used,) that should never be used or even cannot be used, and constructs that come from other worlds.
If you add to that the incompatibilities of APIs and models between browsers, it's easy to understand why Javascript is still often considered as a toy language.
So don't be surprised by the number of things that should not be used or touched in Javascript.
Javascript is not a toy language, nor a poor OO language.
In fact, in a way, Javascript is more OO than Java is ;).
In Javascript (almost) everything is object (think instances, forget about classes!).
Here's a list of the things that are NOT objects in Javascript:
- numbers
- strings
- booleans
- null
- undefined
Everything else is objects.
Yes, everything.
Keep that in mind, and remember (again) that I said Object (instance), not Class.
One really clever thing that was done in javascript is the merging of Objects and Hashtable.
A new Object is an empty container of key/value pairs.
For example, one can create an object using:
var myObject={};
As Javascript is loosely typed, there is no class definition. You just created an Object with nothing in it.
var user={
firstname="Pierre-Antoine"
,'lastname'='Grégoire'
,age=31
};
As you can see, it's quite easy to create an object using key/values.
As you also probably noticed, keys can be String, therefore allowing access to members in two ways:
- using the dot notation: user.firstName
- using the subscript notation: user["firstname"]
This is it for this first short part. Next blog entry will introduce the functional nature of Javascript.

1 commentaires:
Excellent idea: debunking JavaScript's myths and prejudices is really important, as this language now runs in the JVM directly.
Enregistrer un commentaire