Class: Object

ol.Object

Abstract base class; normally only used for creating subclasses and not instantiated in apps. Most non-trivial classes inherit from this.

This extends ol.Observable with observable properties, where each property is observable as well as the object as a whole.

Classes that inherit from this have pre-defined properties, to which you can add your owns. The pre-defined properties are listed in this documentation as 'Observable Properties', and have their own accessors; for example, ol.Map has a target property, accessed with getTarget() and changed with setTarget(). Not all properties are however settable. There are also general-purpose accessors get() and set(). For example, get('target') is equivalent to getTarget().

The set accessors trigger a change event, and you can monitor this by registering a listener. For example, ol.View has a center property, so view.on('change:center', function(evt) {...}); would call the function whenever the value of the center property changes. Within the function, evt.target would be the view, so evt.target.getCenter() would return the new center.

You can add your own observable properties with object.set('prop', 'value'), and retrieve that with object.get('prop'). You can listen for changes on that property value with object.on('change:prop', listener). You can get a list of all properties with object.getProperties().

Note that the observable properties are separate from standard JS properties. You can, for example, give your map object a title with map.title='New title' and with map.set('title', 'Another title'). The first will be a hasOwnProperty; the second will appear in getProperties(). Only the second is observable.

Properties can be deleted by using the unset method. E.g. object.unset('foo').

new ol.Object(opt_values) experimental

src/ol/object.js, line 104
Name Type Description
values Object.<string, *>

An object with key-value pairs.

Fires:

Subclasses

Extends

Methods

changed() inherited experimental

src/ol/observable.js, line 52

Increases the revision counter and dispatches a 'change' event.

Fires:
  • change experimental

get(key){*}

src/ol/object.js, line 150

Gets a value.

Name Type Description
key string

Key name.

Returns:
Value.

getKeys(){Array.<string>}

src/ol/object.js, line 164

Get a list of object property names.

Returns:
List of property names.

getProperties(){Object.<string, *>}

src/ol/object.js, line 174

Get an object of all property names and values.

Returns:
Object.

getRevision(){number} inherited experimental

src/ol/observable.js, line 62
Returns:
Revision.

on(type, listener, opt_this){goog.events.Key} inherited

src/ol/observable.js, line 75

Listen for a certain type of event.

Name Type Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

this Object

The object to use as this in listener.

Returns:
Unique key for the listener.

once(type, listener, opt_this){goog.events.Key} inherited

src/ol/observable.js, line 88

Listen once for a certain type of event.

Name Type Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

this Object

The object to use as this in listener.

Returns:
Unique key for the listener.

set(key, value)

src/ol/object.js, line 203

Sets a value.

Name Type Description
key string

Key name.

value *

Value.

setProperties(values)

src/ol/object.js, line 216

Sets a collection of key-value pairs. Note that this changes any existing properties and adds new ones (it does not remove any existing properties).

Name Type Description
values Object.<string, *>

Values.

un(type, listener, opt_this) inherited

src/ol/observable.js, line 101

Unlisten for a certain type of event.

Name Type Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

this Object

The object which was used as this by the listener.

unByKey(key) inherited

src/ol/observable.js, line 114

Removes an event listener using the key returned by on() or once(). Note that using the ol.Observable.unByKey static function is to be preferred.

Name Type Description
key goog.events.Key

The key returned by on() or once().

unset(key)

src/ol/object.js, line 229

Unsets a property.

Name Type Description
key string

Key name.