Working with properties
The ArcGIS Maps SDK for JavaScript supports a simple, consistent way of getting and setting all properties.
All properties in each class may be set in the constructor. For more information about how this works, please refer to the Constructors section in the Programming Patterns topic.
Getting properties
One way to access a property value is to reference it directly from the object. See the example below regarding how to get the title of a map's basemap using this method.
You may also get properties using the get()
function. The get()
function automatically checks to see if the object exists prior to getting the desired property, so there's no need to check it yourself. In the prior example we checked this using if (map.basemap)
. In the snippet below, get()
does this work for you.
Setting properties
To set a property value, it is possible to directly set it on the object as shown below.
It also possible to set multiple properties at once by calling the set()
method.
Prior to 4.x, some properties could be get (read) or set (write) by calling getMethodname() or setMethodname(). These types of methods are no longer needed as the API supports a simple, consistent way of getting and setting all properties.
Watching properties
Prior to 4.x, watching property changes were handled via events.
Watching property changes are handled with the .watch(property, callback)
method. The callback is called each time the watched property changes. For convenience, it is also possible to watch for a nested property, such as basemap.title
.
For example, in the snippet below, we set up a handler to watch for changes to the title of the map's basemap.
If the user changes the basemap to the following:
The console will log the following information based on the watch handler:
Not all properties can be watched. This is true for any property of type Collection. Instead of watching, developers can register an event handler to be notified of changes to the collection. See the change event documentation for more details.
Additional information
Please refer to these additional links for further information: