Skip to content

JavaScript as structure

A model saved as real JavaScript (a .model.js file) is edited as structure, like any other model. The JavaScript language (lang-js, imported by JS Grammar Demo and JS Tests) gives every JavaScript construct its own concept, so a switch, a labeled loop, a?.b, a class with fields and accessors, or an import has cells you can navigate, complete, check and run, instead of a block of plain text.

Open the JavaScript tour sample (Load Sample…) to see them in one program, and press Run.

What is modelled

Area Constructs
Control flow switch / case / default, do…while, for (init; test; update), for…in, for…of (patterns, an existing target, for await), labeled statements and break label / continue label, with, debugger
Expressions optional chaining (a?.b, a?.[i], a?.()), in, instanceof, comma expressions, assignment as an operand (a = b = c) and compound or logical assignment (+=, ??=, …), ++ / --, tagged templates, new, new.target, import.meta, this, super, arguments, typeof, void, delete, regular-expression, undefined, BigInt and out-of-range number literals
Functions arrow functions with a block body or async, parameters with defaults, rest and destructuring patterns, generators (function*), function expressions
Objects methods, getters and setters, computed keys and spread ({ ...x }) in an object literal
Classes instance, static and #private fields, methods, getters and setters, static { } blocks, computed names, extends, super, class expressions
Modules every form of import and export

Anything else is kept as its own source text in a raw cell: nothing is dropped or changed. An info check (Kept as source text) lists the raw cells, so you can see what the editor cannot analyse. The language a model uses decides this: a model that does not import the JavaScript language (a plain Core model saved as .model.js) reads these constructs as raw text.

Editing a switch

Each clause is an item of the switch's list, so the usual list keys apply:

  • Add a case: put the caret on the keyword of the clause that should come after the new one (often default:) and press Shift+Enter. An empty clause slot opens before it. Press Ctrl+Space (or just type) and choose case; the caret is then in the case's value. A new case starts with no statements (falls through): press Enter at the end of its value to write the first one.
  • Alt+Enter on the switch offers Add case or default clause: it opens an empty clause slot (before a trailing default) with the caret in it; choose case or default from the list.
  • Enter at the end of a clause's value goes into its body; at the start of it (or Shift+Enter) it adds a clause before this one.
  • Typing do never turns into a do…while loop on its own, because it is the start of ordinary names (done, double); type dowhile (or pick it from the list) for the loop.

Declarations

A declaration shows its keyword as a cell: const, let or var. Click it and pick another one from the list, or press Alt+Enter and choose Declare with const / let / var. The : type part of a declaration without a type is only drawn while you hover the declaration or have the caret in it.

Running

Run executes the program in the struktJS evaluator with the same value model as every other language: undefined is None, truthiness and == follow Core. Where that differs from real JavaScript, the page below lists it; the generated JavaScript is the real thing: Run Generator and strukts generate write the same JavaScript text the model is saved as (a concept from a third language in the same file is written in its JavaScript form too).

Differences you may notice:

  • There is no prototype chain. in and for…in see an object's own keys (and a list's indexes); inherited names are not listed.
  • A #private field is an ordinary attribute called #name: other code can see it.
  • A class cannot extend a built-in (Error, Array).
  • with (obj) runs by exposing the object's keys as names, and a check warns that it is deprecated and illegal in strict mode.
  • catch ({ message }), catch ([first]) and patterns with renaming, defaults, holes, a rest element or a computed key (catch ({ a: b }), catch ({ [k]: v })) bind their names (they exist in the catch body only).

Typing in the editor

Each construct has an alias in the completion list (dowhile, label, labeled break, classfield, …). Typing the keyword do does not commit a do-while on its own, so names such as done and double can still be typed.

The completion list of a JavaScript or TypeScript language offers only what the text format can spell. Python-only constructs that Core also has (walrus, match, comprehensions, with, global, del, parameter markers * and /, and so on) are not offered: written to a JavaScript file they would only come out as a call-shaped fallback such as walrus(a, b). A file that already contains one still opens and keeps it, and delete x; is the JavaScript delete operator. A value typed straight into the empty item of an object literal is not accepted either: an item is a key: value pair, a spread or a method, and a bare value would be read back as a block.