A key feature of Scala.js is its interoperability with JavaScript code, whichfar exceeds that of many other languages targeting JavaScript. Except of coursefor languages that translate almost literally to JavaScript (e.g.,TypeScript andCoffeeScript).
See for yourself: on the right, Scala.js natively talks to the DOM API, and wecan hardly notice the border between the two languages.
ES6
var xhr = new XMLHttpRequest();
xhr.open("GET",
"https://api.twitter.com/1.1/search/" +
"tweets.json?q=%23scalajs"
);
xhr.onload = (e) => {
if (xhr.status === 200) {
var r = JSON.parse(xhr.responseText);
$("#tweets").html(parseTweets(r));
}
};
xhr.send();
Scala.js
val xhr = new XMLHttpRequest()
xhr.open("GET",
"https://api.twitter.com/1.1/search/" +
"tweets.json?q=%23scalajs"
)
xhr.onload = { (e: Event) =>
if (xhr.status == 200) {
val r = JSON.parse(xhr.responseText)
$("#tweets").html(parseTweets(r))
}
}
xhr.send()
The following sections explain all the details: