Reusable Objects
Reusable Objects
In the simple case described in the previous topic, you have to use JavaScript object literal syntax to create new objects. This can be cumbersome if you create many object, or if the objects have many properties. Web Central includes a special Base object that simplifies creating new objects. You can define your own objects as extensions of the Base object:
var MapMarker = Base.extend(...your properties go here...);
Inside the call to the
Base.extend()
function you provide the list of properties in the already familiar object
literal format:
var MapMarker
= Base.extend({
// summary text line, displayed as a tooltip
summary: '',
// HTML content, displayed when user clicks on the marker
content: '',
// X and Y coordinates of the marker on the map
coordinates: ''
});
This creates a definition of your object, but not the object itself. In
object-oriented programming, definitions of objects are called
classes
.
You can think of a class as of a set of instructions that describe what
any typical object looks like.
Now you can create and use multiple objects that use the
MapMarker
class:
var marker1 =
new MapMarker();
marker1.summary = "HQ";
marker1.description = "<h1>Headquarters Building</h1>";
marker1.coordinates = "42.37 71.03";
displayMarker(map, marker1);
var marker2 = new MapMarker();
... set marker2 properties ...
displayMarker(map, marker2);