What is the difference between scala.js vs jscala?

落爺英雄遲暮 提交于 2019-12-03 11:13:03

问题


There are two tools to compile Scala code right in the JavaScript: Scala.js and JScala. Both of them look great, and can work with non-trivial Scala code.

What is the technical difference between them?


回答1:


Actually there are more than two tools, but Scala.js and JScala are indeed the 2 tools still under active development.

The difference is fundamental: Scala.js is a compiler from Scala to JavaScript, whereas JScala is a macro-based DSL for generating snippets of JavaScript code.

As @Gaurav explained in his answer, Scala.js is the regular Scala compiler, but it emits JavaScript code instead of JVM bytecode. The Scala code you write is never run by a JVM. All your Scala code is translated to JavaScript. For that reason, it supports the entire Scala language, including, e.g., the collections library. This is useful to write the entire client-side as a Scala application.

Using JScala, on the other hand, most of your app (at least everything outside calls to the javascript macro) is still run on a JVM. The macro rewrites the Scala-looking code inside it to the corresponding JavaScript AST (or directly as a code string). Which means that when the JVM executes the code there, the program generates (at runtime) a JavaScript AST/string. This is useful to generate snippets of code to be sent to the client dynamically.

The main decision criterium is thus:

  • If you want to write the client-side of your Web app as a Scala application, use Scala.js
  • If you want to generate snippets of JavaScript code from the server side written in Scala, use JScala

A more elaborate comparison of these two tools is available at http://www.sebnozzi.com/84/scala-to-javascript-comparison/




回答2:


js-scala is a Scala library for generating JavaScript code, while scala.js is a JavaScript backend to the Scala compiler.

scala.js compiles your Scala code into JavaScript code. It is just the usual Scala compiler that takes your Scala source files and produces JavaScript code instead of JVM bytecode.

.scala| === scalac ===> | .js

On the other hand, js-scala is a Scala library providing composable JavaScript code generators. You can use them in your usual Scala programs to write JavaScript program generators. Your Scala program is compiled to JVM bytecode using the usual Scala compiler and the execution of this program generates a JavaScript program.

.scala| === scalac ===> |.class | === java ===> | .js

For further details, I recommend readint blog by one of authors of js-scala http://julien.richard-foy.fr/blog/2013/12/19/js-scala-and-scala-js/



来源:https://stackoverflow.com/questions/22138601/what-is-the-difference-between-scala-js-vs-jscala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!