openlayers/ol3 — pull request #1213 by pagameba (2013-11-02 01:21:07+00:00)
Title: convert element accessor method into an attribute.

In the last hangout, I asked about why `ol.CollectionEvent` has a `getElement` method rather than an `element` attribute (like `type` and `target`). The answer was two-fold:

1. `type` and `target` are Event properties and `goog` doesn't rename Event properties.
1. advanced compilation renames everything that isn't exported
1. there doesn't seem to be a mechanism for exporting attributes

I think that this PR represents a way to export object properties in a way that is safe with advanced compilation. It is presented primarily for discussion.

Some thoughts about this:

* any code compiled with ol3 needs to use `obj['name']` (or more compressable `obj[symbolRepresentingName]` instead of `obj.name` when accessing properties this ways.
* this isn't a problem inside ol3 but it is a documentation issue for developers using ol3 and compiling their applications with ol3 in advanced mode (if they get used to obj.name)
* documentation with the property and in a FAQ (why does my code fail in advanced mode?) *should* address this I think
* it is much clearer for casual and non-advanced compilation to access object properties this way
* it is much easier to document events that are set up this way

I have an idea about a more complicated internal mechanism that might avoid the need to use `obj['name']` with advanced compilation.

```javascript
this.element = opt_elem;

if (!goog.isDef(this['element'])) {
var self = this;
Object.defineProperty(self, 'element', {
get: function() { return self.element; },
enumerable: true
});
}
```

Using this code inside the `ol.CollectionEvent` constructor avoids the need to export the property at all so there is no `ol.CollectionEventType` enum and no `goog.defineProperty`. Accessing the property with `event.element` works in advanced compiled code (element gets renamed to an internal value) and external, uncompiled code (via the getter) and in whitespace code (no renaming, no property gets defined).

One test for `ol.Collection` fails with this, I haven't figured out why yet so I didn't push the change.