Why aren't interpolation values in tagged template calls saved as a cached array?

#!/JoePea joe at trusktr.io
Sun Jul 26 17:49:50 UTC 2020


What I mean is,

We can currently do this (try it in console):

```js
arrays = []

function html(parts) { arrays.push(parts) }

let n = 0
let n2 = 0

function render() { return html`1st: ${n++}, 2nd: ${n2++}.` }

render()
render()

console.log(arrays[0] === arrays[1]) // true! <------------------- This!
```

But why don't specs allows us to do the following as well? (don't run
it, it doesn't work as intended)

```js
partsArrays = []
valuesArrays = []

function html(parts, values) {
    partsArrays.push(parts)
    valuesArrays.push(values)
}

let n = 0
let n2 = 0

function render() { return html`1st: ${n++}, 2nd: ${n2++}.` }

render()
render()

console.log(partsArrays[0] === partsArrays[1]) // true!
console.log(valuesArrays[0] === valuesArrays[1]) // This would be
convenient too! I think?
```

Instead, if we want an array of the values, we have to use `arguments`
or `...args`. For example:

```js
// ... same ...
function html(parts, ...values) {
    partsArrays.push(parts)
    valuesArrays.push(values)
}
// ... same ...
console.log(partsArrays[0] === partsArrays[1]) // true!
console.log(valuesArrays[0] === valuesArrays[1]) // false! New array every time.
```

Seems like it would've been great to have the cached values arrays
too. Why isn't this the case?

#!/JoePea


More information about the es-discuss mailing list