Index: > A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Business Industries Finance Tax

Home > JavaScript


First Prev [ 1 2 3 4 5 ] Next Last

5 Language elements

5.1 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.

5.2 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 Array, Boolean, Date, Function, Math, Number, Object, RegExp and String. 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. This means 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()

5.3 Data structures

A typical data structure is the Array, which is a map from integers to values. In Javascript, all objects can map from integers to values, but Arrays are a special type of objects that has extra behavior and methods specializing in integer indices (e.g., join, slice, and push).

Arrays have a length property that is guaranteed to always be larger than the largest integer index used in the array. It is automatically updated if one creates a property with an even larger index. Writing a smaller number to the length property will remove larger indices. This length property is the only special feature of Arrays, that distinguishes it from other objects.

Elements of Arrays may be accessed using normal object property access notation:

myArray[1] myArray["1"]

These two are equivalent. Its not possible to use the "dot"-notation or strings with alternative representations of the number:

myArray.1 (syntax error) myArray["01"] (not the same as myArray[1])

Declaration of an array can use either an Array literal or the Array constructor:

myArray = [0,1,,,4,5]; (array with length 6 and 4 elements) myArray = new Array(0,1,2,3,4,5); (array with length 6 and 6 elements) myArray = new Array(365); (an empty array with length 365) Arrays are implemented so that only the elements defined use memory; they are " sparse arrays". Setting myArray[10] = 'someThing' and myArray[57] = 'somethingOther' only uses space for these two elements, just like any other object. The length of the array will still be reported as 58.



Non User