Section 5.5. Function As Object


5.5. Function As Object

Whatever can be created using a constructor has properties and methods above and beyond the obvious, and functions are no exception.

The Function object seems to be the JavaScript object that's had the most changes over time. Originally, the arity property provided the number of arguments. This has been replaced by calling the length method off of the function nameor by accessing length on the arguments array. This, itself, used to be accessible via the function name, but now is accessible just as "arguments" within the function call. Example 5-7 demonstrates accessing both Function object properties.

Example 5-7. Examining Function object properties of length and arguments

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Function Object</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <script type="text/javascript"> //<![CDATA[ // invoking third argument as function function funcObject(x,y,z) {    for (var i = 0; i < funcObject.length; i++) {      document.writeln("argument " + i + ": " + arguments[i] + "<br />");    } } funcObject(1,2,3); //]]> </script> </body> </html>

In addition, as you'll see in Chapter 11, when building custom objects, it's the function's ability to reference its own scope through the keyword this that's important for building classes of new objects.

In 1997, I started a set of class objects to manage cross-browser differences. Eventually, I managed these differences by creating objects for the primary browser types at that time: one for IE, one for Netscape, and one for the ongoing efforts with the DOM at the W3Can approach that the Mozilla foundation and eventually most other browsers (including Netscape and IE) would adopt. Using this, I then attached methods to these objects. These objects were then used to wrap every DIV object in the page, which gave me a set of page components with which I could do most things. Remarkably enough, these objects survived various new generations of browsers for several years and still work today, though I am updating them to be more efficient and take advantage of some of the newer specifications.

These objects worked by defining a model-specific object, such as the following abbreviated example from the DOM (Mozilla/W3C) object function:

function dom_object(obj) {         this.css1 = obj;         this.name = obj.id;         this.objResizeBy = domResizeBy;         this.objHide = ieobjHide;         this.objShow = ieobjShow;         this.objGetLeft = domGetLeft;         this.objGetTop = domGetTop;         this.objSetTop = domSetTop;         this.objSetLeft = domSetLeft; ... }

The same properties can be added to each model implementation, which allows you to hide the browser differences, because each custom object method is assigned a different browser or model-specific function. Handy thing, this. Almost as handy as a JavaScript function.




Learning JavaScript
Learning JavaScript, 2nd Edition
ISBN: 0596521871
EAN: 2147483647
Year: 2006
Pages: 151

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net