Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/calling-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,29 @@ or as
js.Dynamic.literal("foo" -> 42, "bar" -> "foobar")
{% endhighlight %}

### Literal object construction using an Scala object interface
Sometimes for a nicer interface, literal objects can be implimented using
a trait interface.
The above JavaScript code can be implemented using following code:

{% highlight scala %}
trait MyObject extends js.Object {
val foo: Int = js.native
val bar: String = js.native
}
{% endhighlight %}

A Scala object should be added for typesafe creation, it would help the readability
of the code by removing lots of `js.Dynamic.literal` all over the code.

{% highlight scala %}
object MyObject{
def apply(foo: Int, bar: String): MyObject = {
js.Dynamic.literal(foo = foo, bar = bar).asInstanceOf[MyObject]
}
}
{% endhighlight %}

## Defining JavaScript interfaces with traits

Most JavaScript APIs work with interfaces that are defined structurally. In
Expand Down