JavaScript API Reference/Creating Custom Java Script Objects

Reusable Objects with Functions

Objects can also contain functions. It is frequently said that functions defined inside an object encapsulate that object’s data and behavior. The set of functions defined within an object provides a sort of a façade, though which an object exposes its functionality to the application code. Such an object becomes self-sufficient and can be easily used in different part of the application.

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: '' ,

// constructor function
constructor: function(summary, content, coordinates) {
this.summary = summary;
this.content = content;
this.coordinates = coordinates;
},

// displays the marker on the map
display: function(map) {
var coordinates = this.coordinates;
... the code that draw the marker goes here ...
}
});


Object functions are called using the name of the object variable as a prefix:

var marker1 = new MapMarker(
"HQ", "<h1>Headquarters Building</h1>", "42.37 71.03");
marker1.display(map);