8. JavaScript interoperability
JavaScript and Squiggle are friends. Because Squiggle just compiles down to JavaScript files, it's trivial to call Squiggle code from JavaScript, or vice- versa. Squiggle uses all the same data as JavaScript, so you don't even have to convert anything.
Arity problems
Squiggle functions check their arity, but many JavaScript functions are built assuming variadic functions. For example:
# File: helpers.sqg
def add(x) do
x + 1
end
export add
// File: main.js
var helpers = require("./helpers");
var xs = [1, 2, 3].map(helpers.add);
console.log(xs);
This will fail because Array.prototype.map
actually passes three parameters to its callback function: data, index, and array. In scenarios like this, you can wrap the Squiggle function like so:
var xs = [1, 2, 3].map(function(data) {
return helpers.add(data);
});
console.log(xs);
Mutability problems
Sometimes you need mutable data. Fortunately, it's still possible to create it in Squiggle. Simply put a &
before your array or object literals.
let {Object, console} = global
let a = &["sup", "cool"]
let o = &{key: "nothing", key2: "interesting"}
Object.assign(a, {"0": "hi"})
Object.assign(o, {key: "value"})
console.log([a, o])
#=> [["hi", "cool"], {key: "value", key2: "interesting"}]
Problems with this or new
Squiggle does not feature the keywords this
or new
from JavaScript because they cause more harm than good, and are not necessary. Some libraries require their use, however, so Squiggle has functions for dealing with this.
Remember that any Squiggle function can still use JavaScript's this
value if it's explicitly declared as a named parameter with an @
prefix:
jQuery("something").on("click", fn(@this) do
jQuery(this).somethingElse()
end)
If you need to supply the value of this
to a function, you can simply use the
standard JavaScript function methods .apply
or .call
:
someFn.call(myThisValue, param1, param2)
someFn.apply(myThisValue, [param1, param2])
new
is harder to do away with since many APIs (e.g. Date
and Promise
)
require its use to create an instance. Some way to invoke new
on a function will be provided.