# How to get the previous value of the cell from which a function was called?

**URL:** <https://talk.observablehq.com/t/how-to-get-the-previous-value-of-the-cell-from-which-a-function-was-called/755>\
**Category:** Help\
**Created:** [May 13, 2018, 4:27pm UTC](https://talk.observablehq.com/t/how-to-get-the-previous-value-of-the-cell-from-which-a-function-was-called/755 "2018-05-13T16:27:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![magjac](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/magjac/32/525_2.png) [@magjac](https://talk.observablehq.com/u/magjac)\
**Post date:** [May 13, 2018, 4:27pm UTC](https://talk.observablehq.com/t/how-to-get-the-previous-value-of-the-cell-from-which-a-function-was-called/755/1 "2018-05-13T16:27:14Z")

</div>

**this** is the previous value of a cell. I had hoped it was passed to the called function, but it doesn’t seem so according to [https://beta.observablehq.com/d/5bbb7d8edeea576e](https://beta.observablehq.com/d/5bbb7d8edeea576e).

I’m looking for a way to get the previous value of a cell which calls a string literal function (such as in [https://beta.observablehq.com/@mbostock/graphviz](https://beta.observablehq.com/@mbostock/graphviz)), _in the called function_, without having to pass it as an argument.

---

<div class="post-metadata">

**Author:** ![tom](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tom/32/2262_2.png) [@tom](https://talk.observablehq.com/u/tom)\
**Post date:** [May 13, 2018, 8:10pm UTC](https://talk.observablehq.com/t/how-to-get-the-previous-value-of-the-cell-from-which-a-function-was-called/755/2 "2018-05-13T20:10:24Z")

</div>

Hey Magnus,

We don’t override `this` for function cells, because it has an existing meaning in the language, and because function cells _are_ values. I _think_ what you’re looking for is something like [this fork](https://beta.observablehq.com/d/f6a6687b8ea57589) - you can use the [bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind), [call](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call), and [apply](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) methods to set the _this_ context of a function from its caller, and you can use that technique to pass the caller’s `this` value _to_ the callee. Here are [examples of the various ways to do that](https://beta.observablehq.com/d/3744a337b0032a08). That said, I may have misread your question - let me know if this isn’t the precise intent.

---

<div class="post-metadata">

**Author:** ![magjac](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/magjac/32/525_2.png) [@magjac](https://talk.observablehq.com/u/magjac)\
**Post date:** [May 13, 2018, 8:33pm UTC](https://talk.observablehq.com/t/how-to-get-the-previous-value-of-the-cell-from-which-a-function-was-called/755/3 "2018-05-13T20:33:37Z")

</div>

Thanks Tom.

I’m aware of the methods to set the _this_ context from the caller. What I would like to do is to use the same syntax as @mbostock does in [https://beta.observablehq.com/@mbostock/graphviz:](https://beta.observablehq.com/@mbostock/graphviz:)

```
dot`digraph { a -> b; }`

```

which generates an svg element.

What I’m aiming at is to create a similar function:

```
 adot`digraph { a -> b; }`

```

or rather:

```
 adot`${dotSrc}`

```

which would use d3-graphviz to render animated transitions between graphs when `dotSrc` changes.

For that purpose I would need to retrieve the svg generated from the previous call to `adot` _in the_ `adot` _function itself_.

I’ll cook up a notebook to show the functionality tomorrow.

---

<div class="post-metadata">

**Author:** ![tom](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/tom/32/2262_2.png) [@tom](https://talk.observablehq.com/u/tom)\
**Post date:** [May 13, 2018, 8:44pm UTC](https://talk.observablehq.com/t/how-to-get-the-previous-value-of-the-cell-from-which-a-function-was-called/755/4 "2018-05-13T20:44:43Z")

</div>

Got it - that’s going to be a bit tricky to fix for the general case - one option is to do something like [this](https://beta.observablehq.com/d/30cbe2d57352572f) - close over the div and graphviz instance so that transitions are possible. Downside, of course - and probably why you didn’t already do this - is that this wouldn’t automatically support multiple instances.

---

<div class="post-metadata">

**Author:** ![magjac](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/magjac/32/525_2.png) [@magjac](https://talk.observablehq.com/u/magjac)\
**Post date:** [May 14, 2018, 4:53am UTC](https://talk.observablehq.com/t/how-to-get-the-previous-value-of-the-cell-from-which-a-function-was-called/755/5 "2018-05-14T04:53:38Z")

</div>

Thanks. That gives a pretty decent syntax, but, yes, I would like to support multiple instances.

Is there any other way to get a reference to the cell being evaluated? Through the call stack or similar?

---

<div class="post-metadata">

**Author:** ![magjac](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/magjac/32/525_2.png) [@magjac](https://talk.observablehq.com/u/magjac)\
**Post date:** [May 14, 2018, 11:09pm UTC](https://talk.observablehq.com/t/how-to-get-the-previous-value-of-the-cell-from-which-a-function-was-called/755/6 "2018-05-14T23:09:20Z")

</div>

@tom [This](https://beta.observablehq.com/@magjac/graphviz-animations) is what I ended up with. Any comments are welcome.
