# Modifying a Class instance - OpenLayers map.addInteraction()

**URL:** <https://talk.observablehq.com/t/modifying-a-class-instance-openlayers-map-addinteraction/1346>\
**Category:** Help\
**Created:** [October 20, 2018, 3:41pm UTC](https://talk.observablehq.com/t/modifying-a-class-instance-openlayers-map-addinteraction/1346 "2018-10-20T15:41:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicoazel](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nicoazel/32/1169_2.png) [@nicoazel](https://talk.observablehq.com/u/nicoazel)\
**Post date:** [October 20, 2018, 3:41pm UTC](https://talk.observablehq.com/t/modifying-a-class-instance-openlayers-map-addinteraction/1346/1 "2018-10-20T15:41:38Z")

</div>

Hello, I’m new to Observable and I believe that this question relates to some foundational understanding of how Observable treats class instances and their modification across cells.

I am working with this notebook: [Geo Data Explorer 2.0](https://beta.observablehq.com/@nicoazel/geo-data-explorer-2-0)

I am trying to add a Box Select interaction to my map instance.  
The implementation looks like this, accross 3 cells:

```javascript
Select = ol5.interaction.Select //ref library function
select = new Select() //create new instance of this interaction
map.addInteraction(select) //apply it to my map

```

I created a smaller demonstration where I define a class and try to update an instance of it: [Class Modification Test Cross Cells](https://beta.observablehq.com/@nicoazel/class-modification-test-cross-cells)

In both cases when I try `instance.update(updateValue)` the cell returns `undefined`

Could anyone point me in the right direction or is there documentation that describes what I’m doing wrong?

Thanks!

---

<div class="post-metadata">

**Author:** ![bgchen](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/bgchen/32/784_2.png) [@bgchen](https://talk.observablehq.com/u/bgchen)\
**Post date:** [October 20, 2018, 3:51pm UTC](https://talk.observablehq.com/t/modifying-a-class-instance-openlayers-map-addinteraction/1346/2 "2018-10-20T15:51:13Z")

</div>

The `update` method doesn’t return anything, so `undefined` is what you should expect from that cell.

To check that `update` did what it was supposed to, note that if you click on the Object in the output of the `data` cell (to refresh its display), the `url` has turned into `www.myExample.com`. You can also create a new cell containing just `data.url`, and you’ll see that it also displays `www.myExample.com`.

Indeed, the second-to-last cell shows

> The current dataset is “example” @\< [www.myExample.com](http://www.myExample.com)\>.

which is further evidence that the `url` field has been changed.

---

<div class="post-metadata">

**Author:** ![nicoazel](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nicoazel/32/1169_2.png) [@nicoazel](https://talk.observablehq.com/u/nicoazel)\
**Post date:** [October 20, 2018, 5:12pm UTC](https://talk.observablehq.com/t/modifying-a-class-instance-openlayers-map-addinteraction/1346/3 "2018-10-20T17:12:53Z")

</div>

Thanks bgchen, this makes total sense!

So ‘`undefined`’ is not necessarily cause for concern. Though I am probably still missing something to do with when a cell is reevaluated. I’ll keep exploring this in the [Class Modification Test Cross Cells](https://beta.observablehq.com/@nicoazel/class-modification-test-cross-cells) notebook.

Unfortunately this does not seem to be the source of my Box Select problems in OpenLayers which leaves me a bit stumped…

The console error when attempting the drag select (Ctrl+Drag) is pretty opaque to me:  
`TypeError: this.condition_ is not a function`

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 20, 2018, 5:53pm UTC](https://talk.observablehq.com/t/modifying-a-class-instance-openlayers-map-addinteraction/1346/4 "2018-10-20T17:53:56Z")

</div>

Have you read [How Observable Runs](https://beta.observablehq.com/@mbostock/how-observable-runs)?

If you reach into a value returned by another cell and modify it, it doesn’t typically trigger re-evaluation of any referencing cells. In general, you should try to avoid side-effects (mutation) like this, and instead treat cell values as immutable.

So instead of a cell that defines something, and another cell that modifies it…

```auto
myObj = ({key1: "whatever", key2: "whatever else"})

```

```auto
myObj.key1 = "Something"

```

You’d just define the value you want once, from the beginning:

```auto
myObj = ({key1: "Something", key2: "whatever else"})

```

If you want a value to be controlled interactively, then it’s generally best to define that object as a view:

> **[Introduction to Views](https://beta.observablehq.com/@mbostock/introduction-to-views)**
>
> An Observable notebook by Mike Bostock.

For example, if you want to let the user choose because those two keys, you could say something like this:

```auto
viewof key1 = html`<select>
  <option>whatever
  <option>Something
</select>`

```

```auto
myObj = ({key1, key2: "whatever else"})

```

Now whenever the user changes the selected option in the _key1_ drop-down menu, it triggers a re-evaluation of _myObj_, and anything referencing _myObj_ (or _key1_) will be automatically re-evaluated. But the important point is that _myObj_ is not being mutated by _key1_—a new value is generated automatically when _key1_ changes, and _key1_ doesn’t know anything about who references it.

There are use cases for mutation across cells (as you found with [Views Are Mutable Values](https://beta.observablehq.com/@mbostock/views-are-mutable-values), and [Introduction to Mutable State](https://beta.observablehq.com/@jashkenas/introduction-to-mutable-state)). But in general I would caution against reaching for mutable state. Yes, it’s probably what you’re familiar with coming from JavaScript, but usually you can avoid it, and by avoiding or at least minimizing it you end up with simpler code that is easier to understand.

---

<div class="post-metadata">

**Author:** ![mbostock](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mbostock/32/9_2.png) [@mbostock](https://talk.observablehq.com/u/mbostock)\
**Post date:** [October 20, 2018, 6:24pm UTC](https://talk.observablehq.com/t/modifying-a-class-instance-openlayers-map-addinteraction/1346/5 "2018-10-20T18:24:41Z")

</div>

Here’s an updated OpenLayers minimal example:

> **[Hello, OpenLayers!](https://beta.observablehq.com/@mbostock/hello-openlayers)**
>
> An Observable notebook by Mike Bostock.

And here’s an example that uses the Select interaction, exposing the selected features as an array:

> **[Hello, OpenLayers Select!](https://beta.observablehq.com/@mbostock/hello-openlayers-select)**
>
> An Observable notebook by Mike Bostock.

And here’s box selection:

> **[Hello, OpenLayers Box Selection!](https://beta.observablehq.com/@mbostock/hello-openlayers-box-selection)**
>
> An Observable notebook by Mike Bostock.

---

<div class="post-metadata">

**Author:** ![nicoazel](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/nicoazel/32/1169_2.png) [@nicoazel](https://talk.observablehq.com/u/nicoazel)\
**Post date:** [October 21, 2018, 1:39am UTC](https://talk.observablehq.com/t/modifying-a-class-instance-openlayers-map-addinteraction/1346/6 "2018-10-21T01:39:13Z")

</div>

Thanks for the break down, and the OpenLayers examples!. 🍻
