This section has a listing of global objects available to the JavaScript language. You may also want to see DOM Objects for additional objects available in many JavaScript systems.

Global Objects

In JavaScript there are three major categories of objects in the global scope that come with the language: Standard, Error, and LiveConnect. The Standard and Error are part of the ECMA-262 standard for ECMAScript, while LiveConnect is not. LiveConnect is an API that allows JavaScript and Java to communicate.

These global objects should be distinguished from the global object, which in most environments is the window object (representing the window or tab of the browser), which in turn is the parent of the document object (representing the HTML, XHTML, and XML document). See DOM Objects for info about objects available in many JavaScript systems.

JavaScript Global Objects
Standard Error LiveConnect
  1. Array
  2. Boolean
  3. Date
  4. Function
  5. Math
  6. Number
  7. Object
  8. RegExp
  9. String
  1. Error
  2. EvalError
  3. RangeError
  4. ReferenceError
  5. SyntaxError
  6. TypeError
  7. URIError
  1. Java
  2. JavaArray
  3. JavaClass
  4. JavaObject
  5. JavaPackage
  6. netscape
  7. Packages
  8. sun

All JavaScript global objects (except for the Math object which only inherits Object.prototype.constructor) have these members:

.prototype

JavaScript is object-oriented but is prototype-based instead of class-based. In class-based object-oriented programming languages, an object can only be instantiated via a class and its class constructor. The class defines members (properties in a structure and methods) as well as inheritance (behavior reuse). In prototype-based object-oriented programming languages, a "class" is simply a function object (functions are objects in JavaScript) called a prototype constructor. Prototypes are more flexible than classes and hence the distinction.

FYI: The root "proto" is from the Greek for "from", so "from type" as in "archetype".

A prototype constructor function can be used to make instances of itself, and can be modified.

function Dog() {
    this.avm = 'animal';
    this.speak = function(){ alert('bow wow!') }
}
var fred = new Dog();

//The .prototype property can modify the prototype ("class")
Dog.prototype.speak = function(){ alert('ruff ruff!') }
Dog.prototype.legs = 4;

//The .constructor property is inherited from Function.prototype
//.constructor provides a reference to the constructor function
fred.constructor==Dog; //true

HOWEVER, JavaScript also provides a way to instantiate objects ex nihilo ("from nothing"), i.e. without a "class" or prototype, by using literal object notation {}, aka JavaScript Object Notation (JSON). FYI: JSON is also an data interchange format that is human and computer readable comparable to XML.

var fred = {
    avm: 'animal',
    speak: function(){ alert('bow wow!') }
}
fred.prototype.legs = 4; //errs. prototype unapplicable
fred.constructor==fred; //false. The "constructor" is native code

Properties and methods can be added to the prototype (prototype extension) or to an instance (instance extension).

Miscellany

While there are 9 standard global objects in JavaScript, there are only 6 types in JavaScript (string, number, boolean, object (includes null, arrays, and regexps), function, or undefined, as can be show with the typeof operator), there can be a large variety of objects which can also include the JavaScript Standard objects. In an ideal world null, array, and regexp would be their own types in order to match the JS Standard objects.

One useful techqnique is to check the constructor of an object. The following should all alert true.

var ary=[1]; alert(ary.constructor==Array);
var bln=false; alert(bln.constructor==Boolean);
var dtm=new Date(); alert(dtm.constructor==Date);
var fun=function(n){return n+n}; alert(fun.constructor==Function);
var mth=Math.PI; alert(mth.constructor==Number); //Math is exceptional
var num=1; alert(num.constructor==Number);
var obj={a:1}; alert(obj.constructor==Object);
var reg=/1/gi; alert(reg.constructor==RegExp);
var str='1'; alert(str.constructor==String);

Page Modified: (Hand noted: 2008-08-14 17:36:42Z) (Auto noted: 2008-09-17 22:12:41Z)