JavaScript API Reference/Creating Custom Java Script Objects

Reusable Objects with Constructors

Well-designed objects should provide a way to create them, and set all property values, in one step. This simplifies the code that uses objects, and also helps to ensure that objects are created in a consistent manner.

You can provide one-step object creating using a constructor function:

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: '' , // now you need a comma here

// constructor function (the name is important!)
constructor: function(summary, content, coordinates) {
this.summary = summary;
this.content = content;
this.coordinates = coordinates;
}

});


Inside the constructor, the object’s properties are accessed using " this. " prefix.

If the constructor function is defined, it will be called automatically when the object is created using new operator.

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