问题
I was reading this excellent article on V8, Google's Javascript engine: https://developers.google.com/v8/design#mach_code.
At one point, they say that Javascript is compiled directly into machine language, without any bytecode or an interpreter.
To quote:
V8 compiles JavaScript source code directly into machine code when it is first executed. There are no intermediate byte codes, no interpreter.
So, why is Javascript still listed along with the "scripting" and "interpreted" languages, when it is clearly compiled (in V8, at least)?
Edit: can I somehow create an executable out of Javascript, if it is compiled? That would require somehow linking it to V8?
Considering that question, I found this quote:
V8 can run standalone, or can be embedded into any C++ application.
Here: http://code.google.com/p/v8/.
回答1:
This is why "interpreted language" and "compiled language" are examples of sloppy terminology. Whether a language is compiled or interpreted is an attribute of an implementation, not of the language itself.
Many people confuse "dynamically typed languages" (like JavaScript) with "interpreted" and "statically typed language" with "compiled", but these are merely correlations rather than absolutes. It is possible to compile a dynamic language (although it's generally trickier than compiling a static one), and it's possible to interpret a static language (eg: Hugs is an interpreter for Haskell).
回答2:
It is a scripting language because JS code is intended to be supplied and run as source code.
If the coder were to provide a compiled binary for you to execute, then it would not be a script.
Also, no matter what it does on Chrome, the same Javascript source code must also run in other platforms, which may be more or less of a traditional scripting environment. This also doesn't change the nature of the code itself of being a script.
Even if you go to the extreme of compiling it, JS is still a scripting language at heart. There are proper traditional compilers available for virtually every scripting language you can think of (Perl, PHP....); that doesn't stop them from being script languages, nor their source code from being a script.
Likewise, there are interpreters for many languages that are traditionally compiled.
Finally, the issue is further muddied by the concept of "compiling" one language into another. This has been around for a while, but the idea has really taken off with languages like Coffeescript that are intended to compile into Javascript. So what do you call the compiled Coffeescript code?
The terminology isn't really all that helpful, especially now, but the final answer to your question, in the context you're asking it, is that yes, Javascript is still a scripting language.
回答3:
Here, let me demo the code:
later(); // prove that js is a compiling language
function later(num) {
console.log("Your number is: " + num);
}
This piece of code could run both on Chrome Browser and Node js.
If anyone says that js is an interpreted language, then this piece of code would crash, as when u run later(), it should not know the later function body.
This could prove that js is a compiled language, as it compile later function (so machine could know it), then execute it.
来源:https://stackoverflow.com/questions/11420954/can-javascript-be-considered-a-interpreted-language-when-using-google-chrome-v8