疑问
在JS的变量和声明式函数的提升看到了“预编译/预处理/预解释”中“预编译”这个字眼,产生了一个疑问:JS是熟知的解释性语言,但JS能被编译吗?
参考
- https://developer.mozilla.org/en-US/docs/Web/JavaScript : JavaScript (JS) is a lightweight interpreted or JIT-compiled programming language with first-class functions.
- https://zh.wikipedia.org/wiki/JavaScript :** 在客户端,JavaScript在传统意义上被实现为一种解释语言,但在最近,它已经可以被即时编译(JIT)执行。**
- https://stackoverflow.com/questions/26709524/javascript-compiled-language
- https://www.quora.com/Is-JavaScript-a-compiled-or-interpreted-programming-language : JavaScript 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.
- https://en.wikipedia.org/wiki/Chrome_V8 : V8 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.
- https://github.com/v8/v8/wiki/Introduction : V8 compiles and executes JavaScript source code, handles memory allocation for objects, and garbage collects objects it no longer needs.
- https://softwareengineering.stackexchange.com/questions/138521/is-javascript-interpreted-by-design
- https://segmentfault.com/a/1190000013126460
- https://www.zhihu.com/question/31415286/answer/58022648
总结
chrome V8可以编译JS to native code
所以JS是一门解释性语言,有编译特性(取决于引擎)。另外,Python是解释性语言,也有编译特性(要查看具体原理)。
一般来说,JS先解析(例如进行变量、函数的提升声明),再运行。