Search code examples
javascriptsyntaxoperatorsobservablehq

Is Observable's "viewof" really an operator?


I'm confused by Observable's use of the word "operator" in their documentation, and wondering whether they have misused it, or I'm simply not understanding.

Observable has a special viewof operator which lets you define interactive values. A view is a cell with two faces: its user interface, and its programmatic value.

viewof text = html.html`<input value="edit me">

The viewof operator named text renders a text field. The value of that field can be accessed elsewhere by calling text.

https://observablehq.com/documentation/cells/observable-javascript

In this context, the bit to the right of = is JavaScript, and the bit to the left is special Observable syntax. text = is what they call a variable assignment (similar-ish to a JS variable declaration).

Adding viewof beforehand changes the semantics of the declaration, similar (to me) to how one might add public or static before declarations in other contexts.

(Observable has another such "operator", mutable).

Is it reasonable to describe this as an "operator"? And if not, what would a better term be?


Solution

  • The term operator doesn't have a very precise definition that holds across all languages, but it usually refers to something that's used in expressions. For example, the ECMAScript spec (which defines what we think of as JavaScript) currently says:

    ECMAScript also defines a set of built-in operators. ECMAScript operators include various unary operations, multiplicative operators, additive operators, bitwise shift operators, relational operators, equality operators, binary bitwise operators, binary logical operators, assignment operators, and the comma operator.

    That's not a definition, and so far as I can tell it's not a complete list (it seems to be missing newer operators; for example, none of its categories seems to cover nullish coalescing and optional chaining, each of which is described as an "operator" in the list of changes made in ECMAScript 2020), but I think it supports the intuition that operators appear in expressions.

    From your explanation, it doesn't sound like viewof meets that criterion, so I agree with your sense that that's not the right term. I think I would say that it belongs to the broader category of "keywords" or "reserved words".