Some types map quite directly (like
String) where others require some conversions.
|scala.Byte||number||integer, range (-128, 127)|
|scala.Short||number||integer, range (-32768, 32767)|
|scala.Int||number||integer, range (-2147483648, 2147483647)|
|subtypes of `js.Any`||themselves||see the facade types guide|
other Scala classes
including value classes
opaque, except for exported methods
undefined) are represented by their natural equivalent in Scala, as shown
provides dedicated definitions.
The class hierarchy for these standard types is as follows:
js.Any +- js.Object | +- js.Date | +- js.RegExp | +- js.Array[A] | +- js.Function | | +- js.Function0[+R] | | +- js.Function1[-T1, +R] | | +- ... | | +- js.Function22[-T1, ..., -T22, +R] | | +- js.ThisFunction | | +- js.ThisFunction0[-T0, +R] | | +- js.ThisFunction1[-T0, -T1, +R] | | +- ... | | +- js.ThisFunction21[-T0, ..., -T21, +R] | +- js.Iterable[+A] | +- js.Iterator[+A] | +- js.Promise[+A] | +- js.Thenable[+A] +- js.Dictionary[A] +- js.Symbol
Note that most of these types are similar to standard Scala types. For example,
js.Array[A] is similar to
js.FunctionN is similar to
scala.FunctionN. However, they are not completely equivalent, and must not be confused.
With the exception of
js.Dictionary[A], these types have
The collection types feature the standard Scala collection API instead, so that
they can be used idiomatically in Scala code.
js.Function and its subtypes
function taking N parameters of types
TN, and returning a value of
There are implicit conversions from
back, with the obvious meaning.
These conversions are the only way to create a
js.FunctionN in Scala.js.
function object which squares its argument.
You can call a
js.FunctionN in Scala.js with the usual syntax:
js.ThisFunction and its subtypes
The series of
js.ThisFunctionN solve the problem of modeling the
of a jQuery object:
Inside the closure, the value of
this is the DOM element currently being
enumerated. This usage of
this, which is nonsense from a Scala point of view,
this can actually be thought of as an additional
parameter to the closure.
In Scala.js, the
this keyword always follows the same rules as in Scala,
i.e., it binds to the enclosing class, trait or object. It will never bind to
this in an anonymous function.
this in Scala.js, it can be made explicit using
js.ThisFunctionN[T0, T1, ..., TN, R] is the type of a
this parameter of type
T0, as well as N
normal parameters of types
TN, and returning a value of type
From Scala.js, the
this parameter appears as any other parameter: it has a
non-keyword name, a type, and is listed first in the parameter list. Hence,
scala.FunctionN is convertible to/from a
The previous example would be written as follows in Scala.js:
Skipping over the irrelevant details, note that the parameter
this. Note also that we have ascribed the
: js.ThisFunction explicitly to make sure that the right implicit
conversion is being used (by default it would convert it to a
If you call a statically typed API which expects a
js.ThisFunction0, this is
The mapping between JS
this and first parameter of a
works in the other direction, i.e., if calling the
apply method of a
js.ThisFunction, the first actual argument is transferred to the called
function as its
this. For example, the following snippet:
will map to
Dynamically typed interface:
want to. The basic entry point is
js.Dynamic.global, which is a dynamically
js.Dynamic.global.Math, which will be typed as a
You can read and write any field of a
js.Dynamic, as well as call any method
with any number of arguments, and you always receive back a
For example, this snippet taken from the Hello World example uses the dynamically typed interface to manipulate the DOM model.
In this example,
newP are all inferred to be of
Literal object construction
can be written in Scala.js either as
Literal object construction using an Scala object interface
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.
Alternatively, you can use anonymous classes extending
js.Object or a
Scala.js-defined JS trait.