Online Book Reader

Home Category

AJAX In Action [291]

By Root 3968 0
it to the prototype for the Object class:

Object.prototype.implements=function(funcName){

return this && this[funcName] && this[funcName] instanceof Function;

}

That allows us to check specific functions by name:

function hasArea(obj){

return obj.implements("getArea");

}

Licensed to jonathan zheng

Objects in JavaScript

605

or even to test for compliance with an entire interface:

function isShape(obj){

return obj.implements("getArea") && obj.implements("getPerimeter");

}

This gives us some degree of safety, although still not as much as we would get under Java. A rogue object might implement getArea() to return a string rather than a numerical value, for example. We have no way of knowing the return type of a JavaScript function unless we call it, because JavaScript functions have no predefined type. (Indeed, we could write a function that returns a number during the week and a string on weekends.) Writing a set of simple test functions to check return types is easy enough; for example:

function isNum(arg){

return parseFloat(arg)!=NaN;

}

NaN is short for “not a number,” a special JavaScript variable for handling number format errors. This function will actually return true for strings if they begin with a numeric portion, in fact. parseFloat() and its cousin parseInt() will try their best to extract a recognizable number where they can. parseFloat("64 hectares") will evaluate to 64, not NaN.

We could firm up our addAreas() function a little further:

function addAreas(s1,s2){

var total=null;

if (hasArea(s1) && hasArea(s2)){

var a1=s1.getArea();

var a2=s2.getArea();

if (isNum(a1) && isNum(a2)){

total=parseFloat(a1)+parseFloat(a2);

}

}

return total;

}

I call parseFloat() on both arguments to correctly handle strings that slip through the net. If s1 returns a value of 32 and s2 a value of 64 hectares, then addAreas() will return 96. If I didn’t use parseFloat, I’d get a misleading value of 3264 hectares!

In summary, duck typing keeps things simple but requires you to trust your development team to keep track of all the details. Duck typing is popular among the Ruby community, who are generally a very smart bunch. As one moves from a single author or small tightly bound team to larger projects involving separate Licensed to jonathan zheng

606

APPENDIX B

JavaScript for object-oriented programmers

subgroups, this trust inevitably weakens. If you want to add a few checks and balances to your code on top of duck typing, then perhaps this section will have shown you where to start.

We’ve looked at the language from the point of view of objects. Now let’s drill down a little to look at these functions that we’ve been throwing around and see what they really are.

B.3 Methods and functions

We’ve been defining functions and calling them in the previous section and in the rest of this book. A Java or C# programmer might have assumed that they were something like a method, defined with a slightly funny-looking syntax. In this section, we’ll take functions apart a bit more and see what we can do with them. B.3.1 Functions as first-class citizens

Functions are a bit like Java methods in that they have arguments and return values when invoked, but there is a key difference. A Java method is inherently bound to the class that defined it and cannot exist apart from that class. A JavaScript function is a free-floating entity, a first-class object in its own right. (Static Java methods lie somewhere in between these two—they are not bound to any instance of an object but are still attached to a class definition.) Programmers who have worked their way through the C family may think

“Ah, so it’s like a function pointer in C++, then.” It is indeed, but that’s not the end of it.

In JavaScript, Function is a type of built-in object. As expected, it contains executable code, and can be invoked, but it is also a descendant of Object, and can do everything that a JavaScript object can, such as storing properties by name. It is quite possible (and quite common) for a

Return Main Page Previous Page Next Page

®Online Book Reader