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

  • While there is no strict definition for the term operator, it usually refers to a syntactical construct that lets you combine an expression from other expressions. It may also involve other constituents though, like when you say that the . operations combines an expression and a name into a property access expression.

    However, var/let/const are typically not considered operators since they don't build an expression, but rather a declaration statement. The viewof in your example appears to fall into the same category. It's just a keyword.