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.
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 |
|
|
|
All JavaScript global objects (except for the Math object which only inherits Object.prototype.constructor) have these members:
.prototype propertyFunction.prototype
.caller.constructor.length. Overridden by Array objects to return number of elements. Overridden by String objects to return the number of characters..nameFunction.prototype
.apply(thisArg[, argArray]).call(thisArg[, arg1[, arg2[, ...]]])Object.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).
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.
Page Modified: (Hand noted: 2008-08-14 17:36:42Z) (Auto noted: 2009-02-11 19:03:26Z)