From 9767692f3b3bb1230b302b4c8fe733f944022687 Mon Sep 17 00:00:00 2001 From: David Barri Date: Mon, 18 Oct 2021 21:07:33 +1100 Subject: [PATCH] Document creating JS functions with varargs --- doc/interoperability/types.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/doc/interoperability/types.md b/doc/interoperability/types.md index 9117bd22..da3997ca 100644 --- a/doc/interoperability/types.md +++ b/doc/interoperability/types.md @@ -273,6 +273,21 @@ var o = new Object(); var x = f.call(o, 4); {% endhighlight %} +### JS functions with varargs + +In order to capture varargs from a JS function, create your own trait that +extends `js.Function` or `js.ThisFunction`. + +{% highlight scala %} +trait JsVarargsFn extends js.Function { + def apply(args: Any*): Unit +} + +val f: JsVarargsFn = { args => + println(s"This method was called with ${args.size} args.") +} +{% endhighlight %} + ## Dynamically typed interface: `js.Dynamic` Because JavaScript is dynamically typed, it is not often practical, sometimes