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 property
  • Inherited properties from Function.prototype
    • .caller
    • .constructor
    • .length. Overridden by Array objects to return number of elements. Overridden by String objects to return the number of characters.
    • .name
  • Inherited methods from Function.prototype
    • .apply(thisArg[, argArray])
    • .call(thisArg[, arg1[, arg2[, ...]]])
  • Inherited methods from Object.prototype
    • .__defineGetter__()
    • .__defineSetter__()
    • .hasOwnProperty(strProperty)
    • .isPrototypeOf(object)
    • .__lookupGetter__()
    • .__lookupSetter__()
    • .__noSuchMethod__()
    • .propertyIsEnumerable(property)
    • .toSource(). Actually each global object overrides this.
    • .toString(). Actually each global object overrides this.
    • .unwatch()
    • .valeuOf()
    • .watch()

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);

Enumerator is an object built into JScript 3+ that is generally not available to other flavors of JavaScript. Enumerator is mostly used for looping through VBScript style collections (which do not have the properties of a typical JavaScript object) when using JScript in server-side ASP. The Enumerator object is best demonstrated by contrasting it with a VBScript example.

''''VBScript example
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("C:\Text")
Set FileCollection = Folder.Files
Response.Write("Number of files found: " & FileCollection.Count & "<br />")
For Each File in FileCollection
   Response.Write("File.Name: " & File.Name & "<br />")
Next
'De-reference the objects
Set FileCollection = Nothing
Set Folder = Nothing
Set FSO = Nothing
////JScript example
var FSO, Folder, FileCollection, Enum;
FSO = Server.CreateObject("Scripting.FileSystemObject");
Folder = FSO.GetFolder("c:\\Text");
FileCollection = Folder.Files;
Enum = new Enumerator(FileCollection)
Response.Write("Number of files found: " + FileCollection.Count + "<br />");
for(Enum; !Enum.atEnd(); Enum.moveNext()) {
   Response.Write('Enum.item(): ' + Enum.item() + '<br />');
}
//De-reference or destroy the objects
delete Enum;
Enum = null;
FileCollection = null;
Folder = null;
delete FSO;
FSO = null;

Enumerator objects have four methods:

  • atEnd(). Returns true if last in the collection.
  • item(). Returns the current item in the collection.
  • moveFirst(). Moves the current item to the first position in the collection.
  • moveNext(). Moves the current item to the next position in the collection.

Regular JavaScript already has associative array built in, but if you like VBScript style collections, then you can imitate it in your own JavaScript objects: Collection objects in JavaScript [http://www.builderau.com.au/program/javascript/soa/Collection-objects-in-JavaScript/0,339028434,320269242,00.htm]. For more about browser/platform specific features, see places like http://www.c-point.com/javascript_tutorial/javascript_intrinsic_objects.htm or http://www.aptana.com/reference/html/api/JSCore.index.html.



GeorgeHernandez.comSome rights reserved