JavaScript - 编译性还是解释性?

疑问

在JS的变量和声明式函数的提升看到了“预编译/预处理/预解释”中“预编译”这个字眼,产生了一个疑问:JS是熟知的解释性语言,但JS能被编译吗?

参考

  1. https://developer.mozilla.org/en-US/docs/Web/JavaScriptJavaScript (JS) is a lightweight interpreted or JIT-compiled programming language with first-class functions.
  2. https://zh.wikipedia.org/wiki/JavaScript :** 在客户端,JavaScript在传统意义上被实现为一种解释语言,但在最近,它已经可以被即时编译(JIT)执行。**
  3. https://stackoverflow.com/questions/26709524/javascript-compiled-language
  4. https://www.quora.com/Is-JavaScript-a-compiled-or-interpreted-programming-languageJavaScript used to be purely interpreted but that was many years ago. Nowadays, it is JIT-compiled to native machine code in all major JavaScript implementations.
  5. https://en.wikipedia.org/wiki/Chrome_V8V8 compiles JavaScript directly to native machine code before executing it, instead of more traditional techniques such as interpreting bytecode or compiling the whole program to machine code and executing it from a filesystem.
  6. https://github.com/v8/v8/wiki/IntroductionV8 compiles and executes JavaScript source code, handles memory allocation for objects, and garbage collects objects it no longer needs.
  7. https://softwareengineering.stackexchange.com/questions/138521/is-javascript-interpreted-by-design
  8. https://segmentfault.com/a/1190000013126460
  9. https://www.zhihu.com/question/31415286/answer/58022648

总结

chrome V8可以编译JS to native code
所以JS是一门解释性语言,有编译特性(取决于引擎)。另外,Python是解释性语言,也有编译特性(要查看具体原理)。
一般来说,JS先解析(例如进行变量、函数的提升声明),再运行。

posted @ 2018-06-07 00:26  Rocin  阅读(1299)  评论(0编辑  收藏  举报