2. About me
Evgeniy Safronov
I am from Kherson
Software engineer in RIFL Media LLC
I love to write in JS and talk about JS
contacts:
evgeniy.safronov@outlook.com
skype: lambda.omega1
github.com/javacodegeek
6. Scala profile
● started in 2001 at the École Polytechnique Fédérale de Lausanne by Martin
Odersky
● functional programming and object oriented programming
● very strong static type system
● was released in 2004 on JVM and .NET
● v2.0 followed in March 2006
www.scala-lang.org
9. Elitism Scala
Scala developers are expensive
High barrier to entry
The successful combination of functional and object-oriented paradigms
Wide range of applications
Functional programming with immutable variables, lambdas, closures, lists, and so
on
10. Java code
public class Person {
private final String firstName;
private final String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
15. Code examples: collections
const personMap = new Map([
[10, new Person("Roger", "Moore")],
[20, new Person("James", "Bond")]
]);
const names = [];
for (const [key, person] of personMap) {
if (key > 15) {
names.push(`${key} = ${person.firstName}`);
}
}
ES6
val personMap = Map(
10 -> new Person("Roger", "Moore"),
20 -> new Person("James", "Bond")
)
val names = for {
(key, person) <- personMap
if key > 15
} yield s"$key = ${person.firstName}"
Scala.js
16. Compiler output code
object Main extends js.JSApp{
def main() = {
var x = 0
while(x < 10) x += 3
println(x)
// 12
}
}
Scala.js
ScalaJS.c.LMain$.prototype.main__V =
(function() {
var x = 0;
while ((x < 10)) {
x = ((x + 3) | 0)
};
ScalaJS.m.s_Predef$()
.println__O__V(x)
// 12
});
ES5
17. How to install?
install sbt (scala build tool)
config plugin ”sbt-scalajs”
Rhino presents out of the box, but you can setup V8.
http://www.scala-js.org/tutorial/basic/
19. Using the DOM
Adding the DOM library to build.sbt
import org.scalajs.dom
import dom.document
def appendPar(targetNode: dom.Node, text: String): Unit = {
val parNode = document.createElement("p")
val textNode = document.createTextNode(text)
parNode.appendChild(textNode)
targetNode.appendChild(parNode)
}
20. Reacting on User Input
import scala.scalajs.js.annotation.JSExport
@JSExport
def addClickedMessage(): Unit = {
appendPar(document.body, "You clicked the button!")
}
<button id=”click-me-button” type=”button” onClick=”tutorial.webapp.MyApp().
addClickedMessage()”>Click me!</button>
21. Using jQuery
Adding the DOM library to build.sbt
import org.scalajs.jquery.jQuery
jQuery("body").append("<p>[message]</p>")
23. Why you should try Scala.js
New paradigms
All power of Scala
Very good IDE support (IntelliJ Scala and Eclipse ScalaDE)
Scala community
Universal application