Online Book Reader

Home Category

AJAX In Action [290]

By Root 4096 0
object");

}else if (myObj instanceof Array){

alert("it's an array");

}

}

testType([1,2,3,4]);

then we will be told that we have an Object, which is also technically correct but probably not what we intended.

Finally, there are times when we may want to exhaustively discover all of an object’s properties and functions. We can do this using the simple for loop: function MyObject(){

this.color='red';

this.flavor='strawberry';

Licensed to jonathan zheng

Objects in JavaScript

603

this.azimuth='45 degrees';

this.favoriteDog='collie';

}

var myObj=new MyObject();

var debug="discovering...\n";

for (var i in myObj){

debug+=i+" -> "+myObj[i]+"\n";

}

alert(debug);

This loop will execute four times, returning all the values set in the constructor. The for loop syntax works on built-in objects, too—the simple debug loop above produces very big alert boxes when pointed at DOM nodes! A more developed version of this technique is used in the examples in chapters 5 and 6 to develop the recursive ObjectViewer user interface.

There is one more feature of the conventional object-oriented language that we need to address—the virtual class or interface. Let’s look at that now. B.2.6 Interfaces and duck typing

There are many times in software development when we will want to specify how something behaves without providing a concrete implementation. In the case of our Shape object being subclassed by squares, circles, and so on, for example, we know that we will never hold a shape in our hands that is not a specific type of shape. The base concept of the Shape object is a convenient abstraction of common properties, without a real-world equivalent. A C++ virtual class or a Java interface provides us with the necessary mechanism to define these concepts in code. We often speak of the interface defining a contract between the various components of the software. With the contract in place, the author of a Shape-processing library doesn’t need to consider the specific implementations, and the author of a new implementation of Shape doesn’t need to consider the internals of any library code or any other existing implementations of the interface. Interfaces provide good separation of concerns and underpin many design patterns. If we’re using design patterns in Ajax, we want to use interfaces. JavaScript has no formal concept of an interface, so how do we do it?

The simplest approach is to define the contract informally and simply rely on the developers at each side of the interface to know what they are doing. Dave Thomas has given this approach the engaging name of “duck typing”—if it walks like a duck and it quacks like a duck, then it is a duck. Similarly with our Shape interface, if it can compute an area and a perimeter, then it is a shape. Licensed to jonathan zheng

604

APPENDIX B

JavaScript for object-oriented programmers

Let’s suppose that we want to add the area of two shapes together. In Java, we could write

public double addAreas(Shape s1, Shape s2){

return s1.getArea()+s2.getArea();

}

The method signature specifically forbids us from passing in anything other than a shape, so inside the method body, we know we’re following the contract. In JavaScript, our method arguments aren’t typed, so we have no such guarantees: function addAreas(s1,s2){

return s1.getArea()+s2.getArea();

}

If either object doesn’t have a function getArea() attached to it, then we will get a JavaScript error. We can check for the presence of the function before we call it: function hasArea(obj){

return obj && obj.getArea && obj.getArea instanceof Function;

}

and modify our function to make use of the check:

function addAreas(s1,s2){

var total=null;

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

total=s1.getArea()+s2.getArea();

}

return total;

}

Using JavaScript reflection, in fact, we can write a generic function to check that an object has a function of a specific name:

function implements(obj,funcName){

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

}

Or, we can attach

Return Main Page Previous Page Next Page

®Online Book Reader