Online Book Reader

Home Category

AJAX In Action [292]

By Root 4089 0
Function object to have other Function objects attached to it as methods.

We’ve already seen how to get a reference to a Function object. More usually, we would want to reference a function and invoke it in a single line, such as var result=MyObject.doSomething(x,y,z)

However, the Function is a first-class object, and it can also be executed via the call() method (and its close cousin apply()):

var result=MyObject.doSomething.call(MyOtherObject,x,y,z)

or even

Licensed to jonathan zheng

Methods and functions

607

var result=MyObject['doSomething'].call(MyOtherObject,x,y,z)

The first argument of Function.call() is the object that will serve as the function context during the invocation, and subsequent arguments are treated as arguments to the function call. apply() works slightly differently in that the second argument is an array of arguments to pass to the function call, allowing greater flexibility in programmatically calling functions whose argument list length is undetermined.

It’s worth pointing out here that the argument list to a JavaScript function is not of a fixed length. Calling a Java or C# method with more or fewer arguments than it declares would generate a compile-time error. JavaScript just ignores any extra args and assigns undefined to missing ones. A particularly clever function might query its own argument list through the arguments property and assign sensible defaults to missing values, throw an exception, or take any other remedial action. This can be exploited to combine a getter and setter method into a single function, for example:

function area(value){

if (value){

this.area=value;

}

return this.area;

}

If we simply call area(), then value is undefined, so no assignment takes place, and our function acts as a getter. If a value is passed in, our function acts as a setter. This technique is used extensively by Mike Foster’s x library (see the Resources section at the end of this chapter, and also chapter 3), so if you plan on working with that, you’ll soon be familiar with the idiom.

Functions become really interesting, though, when we take advantage of their independence as first-class objects.

B.3.2 Attaching functions to objects

As a functional language, JavaScript allows us to define functions in the absence of any object, for example:

function doSomething(x,y,z){ ... }

Functions can also be defined inline:

var doSomething=function(x,y,z){ ... }

As a concession to object-orientation, functions may be attached to objects, giving the semblance of a Java or C# method. There is more than one way of doing this. Licensed to jonathan zheng

608

APPENDIX B

JavaScript for object-oriented programmers

We can attach a predefined function to a predefined object (in which case only that object can call the function, not any other object derived from the same prototype):

myObj.doSomethingNew=doSomething;

myObj.doSomethingNew(x,y,z);

We can also attach functions such that every instance of a class can access them, by adding the function (either predefined or declared inline) to the new object in the constructor, as we saw in section B.2.2, or by attaching it to the prototype. Once we’ve done this, though, they aren’t very strongly attached, as we will see. B.3.3 Borrowing functions from other objects

Giving functions first-class object status alters the capabilities of a language significantly. Furthermore, an understanding of these alterations is important when coding GUI event handling, so most Ajax programmers would do well to understand it. So what are these new capabilities? Well, first off, one object can borrow another’s function and call it on itself. Let’s define a class to represent species of tree in a taxonomic system:

function Tree(name, leaf, bark){

this.name=name;

this.leaf=leaf;

this.bark=bark;

}

Next, we’ll add a function to it that provides a quick description of the tree: Tree.prototype.describe=function(){

return this.name+": leaf="+this.leaf+", bark="+this.bark;

}

If we now instantiate a Tree object and ask

Return Main Page Previous Page Next Page

®Online Book Reader