JavaScript
Building JavaScript
To build to JavaScript, run a command like keen build foo.keen --out foo.js.
Then include it in a web page using <script type="module" src="/foo.js"></script> .
You can also run node foo.js .
Node.js support is mostly intended for quick testing and not as the main build target.
Calling JavaScript functions
Now we'll write "Hello, world" directly using JavaScript APIs.
The js-any type can hold any JavaScript value.
Basically any function using this type is unsafe.
js-global is equivalent to window (or self) in the browser
or global in Node.js .
subscript reads a property from a JS object.
So js-global["alert"] is equivalent to
window.alert in JS.
call is a JS function call.
So call alert, "Hello, world!"
is equivalent to alert("Hello, world!") in JS.
call returns js-any.
_ = drops the js-any value to get void.
Use JavaScript objects
To call a method on a JavaScript object, use call-property.
This paragraph has ID test-p. After running the above script it will have a border.
The first line above is equivalent to document.querySelector("#test-p") in JS.
The second line is equivalent to home.style.border = "1px solid pink"; in JS.
Conversion
Make JavaScript objects
named-new from keen/js/util
is useful for creating JS objects.
Async
To call a JavaScript async function, simply call and await it.
For best performance, try to avoid doing async things in a deeply nested functions, since this makes their callers (and callers' callers, etc.) async functions too.
Callbacks
A Keen lambda can be converted to a JS function using to.
These functions support lambdas taking void or js-any and returning void or js-any.
Imports
Keen does not yet have the ability to import JavaScript modules directly.
There is a js-import function though.
Other JavaScript operations
If Keen doesn't have a function for doing what you want with JS,
you can write a JS snippet using eval.