有没有像 JavaScript 编译器这样的东西?
我想知道是否可以编写一个 javascript 程序并将其编译并链接到可执行文件中?
如果是这样,是否有可能创建一个与 c/c++ 世界的 libc 等效的 libjs?创建这样的东西不会使 javascript 成为一种成熟的语言,然后可以直接在目标硬件上编译和运行吗?
如果你有一个 javascript 编译器,你就不能用 javascript 编写一个新的编译器吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,你可以写一个js编译器。不确定它有多受欢迎:
现在 js 引擎非常快,所以你没有获得太多速度。
这将是特定于平台的,或者您必须支持多个平台。不太令人愉快。
它有什么用?解释型语言的伟大之处在于它不需要编译。它缩短了开发周期和构建时间(曾经坐在 C 程序前面,不得不更改整个项目所依赖的文件,并且必须运行并重新运行
make
,这需要几分钟才能编译所有内容? ).关于你的最后一点,你是对的。如果你有这些编译器之一,你确实可以用 javascript 编写另一个。
Yes, you could write a js compiler. Not sure how popular it would be:
js engines are very fast these days, so you're not gaining much speed.
It would be platform specific, or you would have to support multiple platforms. Not pleasant.
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
make
s 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.
阅读此 ...并且不要错过评论。
这里还有一些选项。
Read this ... and do not miss the comments.
Here are also some options.
是的,你有一个叫做
Google Closure Compiler
的东西,但它不是传统意义上的编译器,它不会将javascript转换为机器代码,而是将javascript转换为高度优化的javascript。它实际上是一个优化编译器。编译器还运行一些测试来检测拼写错误等错误,就像工具JSLint
一样。但 Google 建议在Closure
库中编写的 javascript 上使用此编译器。有关 Closure Compiler 的详细信息,请参阅此。但我不认为将客户端 javascript 编译为机器代码是一个好主意,因为机器代码是机器相关的,因此在将 javascript 发送到客户端之前,您必须检测其操作系统及其处理器架构。所以这将变得像 linux 上的 javascipt for firefox、windows 上的 javascipt for firefox、x86 上的 javascipt for firefox 等
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 toolJSLint
.But Google advises to use this compiler on javascript written inClosure
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