Introduction
Please note that this tutorial will look best in a standards-compliant browser, such as Firefox .
I agree with Douglas Crockford's analysis that JavaScript is the world's most misunderstood programming language. This page is intended to rectify common misconceptions about JavaScript and reveal its true, often lost capabilities. As always, please e-mail me with any comments: labreuer+javascript@gmail.com . Please note that this page is dedicated to client-side javascript.
References- The official ECMA specification for ECMAScript v3 (poorly written pdf)
- Douglas Crockford: excellent JavaScript documentation and resources
- Very verbose introduction/tutorial from our friends at QuirksMode
All of these statements are incorrect:
- JavaScript is somehow related to Java
- JavaScript is not an object oriented language
- There exists a lot of quality [JavaScript] code on the web
- JavaScript is not suitable for professional programming
It used to be that Internet Explorer implemented JScript, Netscape implemented JavaScript, and the two had very limited overlap. Fortunately, the situation has changed; browser sniffing is out and object sniffing is in. For a long, in-depth discussion, see QuirksMode ; the idea is that you follow the below example:
Here, we don't test to see if the browser is IE 1.2.3.4, or Netscape 3.3.221, we simply ask the question, "Does this property/function/whatever exist?" This way, we don't care about the identity of the browser, we just care about what functionality the browser supports. If the next version of some browser supports something it didn't before, no well-written scripts will need changing. Note that the != undefined is not necessary, but it makes it clear that we are testing for existence of an object member and not a boolean value.
The struct equivalentJavaScript doesn't do structs, but we can come pretty close:
The following is equivalent, but slightly more dangerous due to the
Strictly speaking, a variable is null if it exists but has no value, whereas "something" is null if it's not even a variable. More will be added later; see these notes in the time being.
Funkiness with null and ||The boolean logical or operator, ||, apparently is just for boolean values in javascript. It simply returns the first value that is not null, otherwise ???. (Perhaps it returns false, perhaps null, perhaps undefined.) A particularly common example is setting the event object to be the object that was passed if an object were passed, or window.event, which is what IE uses.
back