Incompatibilities
Javascript, like HTML, is often not compliant to standards, instead being built to work with specific web browsers. The current ECMAScript standard should be the base for all Javascript implementations in theory, but in practice the Netscape (and Mozilla) browsers use JavaScript, Microsoft Internet Explorer uses JScript, and other browsers such as Opera and Safari use other ECMAScript implementations, often with additional nonstandard properties to allow compatibility with JavaScript and JScript.
JavaScript and JScript contain several properties which are not part of the official ECMAScript standard, and may also miss several properties. As such, they are in points incompatible, which requires script authors to work around these bugs. JavaScript is more standards-compliant than Microsoft's JScript, which means that a script file written according to the ECMA standards will work for most browsers, except those based on Internet Explorer.
This also means every browser may treat the same script differently, and what works for one browser may fail in another browser, or even in a different version of the same browser. Like with HTML, it is thus advisable to write standards-compliant code.
MSIE's VBScript is not JavaScript, and it is incompatible with the ECMA standard.
Language elements
Variables
Variables are generally dynamically typed.
Variables are defined by either just assigning them a value or by using the var statement.
Variables declared outside of any function are in "global" scope, visible in the entire web page; variables declared inside a function are local to that function.
To pass variables from one page to another, a developer can set a cookie or use a hidden frame or window in the background to store them.
Objects
Everything in JavaScript is either a primitive value or an object. Objects
are entities that have an identity (they are only equal to themselves) and that map property names to values.
That is, an object is an associative array similar to hashes in the Perl programming language, or dictionaries in Python, PostScript and Smalltalk.
JavaScript has several kinds of built in objects, namely object, Array, string, Number, Boolean, Function, Date and Math.
Other objects are "host objects", defined not by the language but by the runtime environment. In a browser, typical host objects belong to the DOM (window, form, links etc.).
By defining a constructor function it is possible to define objects.
JavaScript is a prototype based object-oriented language. That mean
that inheritance is between objects, not between classes (Javascript has no classes). Objects inherit properties from their prototypes.
One can add additional properties or methods to individual objects after they have been created.
To do this for all instances created by a single constructor function, one can use the prototype property of the constructor to access the prototype object.
Example: Creating an object
// constructor function
function MyObject(attributeA, attributeB) {
this.attributeA = attributeA
this.attributeB = attributeB
}
// create an Object
obj = new MyObject('red', 1000)
// access an attribute of obj
alert(obj.attributeA)
// access an attribute with the associative array notation
alert(obj["attributeA"])
Object hierarchy can be emulated in JavaScript. For example:
function Base()
{
this.Override = _Override;
this.BaseFunction = _BaseFunction;
function _Override()
{
alert("Base::Override()");
}
function _BaseFunction()
{
alert("Base::BaseFunction()");
}
}
function Derive()
{
this.Override = _Override;
function _Override()
{
alert("Derive::Override()");
}
}
Derive.prototype = new Base();
d = new Derive();
d.Override();
d.BaseFunction();
will result in the display:
Derive::Override()
Base::BaseFunction()
Elements of Arrays may be accessed using normal object property access notation:
Every function is an instance of Function, a type of base object. Functions can be created and assigned like any other objects: