Is there such a thing as a javascript compiler?

坚强是说给别人听的谎言 提交于 2021-02-20 05:12:47

问题


I'm wondering if its possible to write a javascript program and have it compiled and linked into an executable?

If so would it be possible to create a libjs that would be the equivalent of libc for the c/c++ world? wouldn't creating something like this make javascript a full fledged language that could then be compiled and run directly on the target hardware?

If you had a compiler for javascript, couldn't you write a new compiler in javascript?


回答1:


Yes, you could write a js compiler. Not sure how popular it would be:

  1. js engines are very fast these days, so you're not gaining much speed.

  2. It would be platform specific, or you would have to support multiple platforms. Not pleasant.

  3. What would it be useful for? The great thing about an interpreted language is the very fact that it doesn't need to be compiled. It shortens development cycles and build times (ever sat in front of a C program and had to change a file that the entire project relies on and had to run and rerun makes that take minutes to compile everything?).

Regarding your last point, you're correct. Had you one of these compilers, you could indeed write another one in javascript.




回答2:


Read this ... and do not miss the comments.

Here are also some options.




回答3:


Yes you have something called Google Closure Compiler but its not a compiler in the conventional sense,it doesnt convert javascript into machine code but converts javascript into javascript but highly optimized javascript. Its actually an optimizing compiler.Also the compiler runs some tests to detect errors like typos much like the tool JSLint.But Google advises to use this compiler on javascript written in Closure Library. see this for more on Closure Compiler.

But i dont think compiling client-side javascript to machine code is a good idea because machine code is machine dependent so then before you send javascript to the client you have to detects its OS and its processor architecture. So this would become like javascipt for firefox on linux,javascipt for firefox on windows,javascipt for firefox on x86,etc



来源:https://stackoverflow.com/questions/6934981/is-there-such-a-thing-as-a-javascript-compiler

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