为什么我不能在Node.JS终端上使用c ++语法?

Jay*_*eth -7 javascript c++ v8 node.js

我总是想知道为什么我不能在Node.js终端上使用C++语法.

例如,V8是一个实现ECMAScript的JavaScript引擎.Node.js本身是一个C++程序,它告诉V8引擎使用它的功能.所以这些函数是用C++代码编写的,因此这个应用程序应该能够采用C++本机语法.这意味着如果我要在Node.js终端中键入C++代码,那么Node.js终端应该能够将这些语法传递给Node.js核心中的C++模块并成功输出.但相反,它给出了:

SyntaxError: Unexpected identifier
at Object.exports.createScript (vm.js:24:10)
at REPLServer.defaultEval (repl.js:236:25)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.<anonymous> (repl.js:441:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:188:7)
at REPLServer.Interface._onLine (readline.js:224:10)
at REPLServer.Interface._line (readline.js:566:8)
at REPLServer.Interface._ttyWrite (readline.js:843:14)
Run Code Online (Sandbox Code Playgroud)

我只是在寻找一个清晰的解释,虽然Node.js是在C++之上编写的,但是这两种语言是如何被抽象的.

Ben*_*ley 6

仅仅因为程序是用C++编写的,并不意味着它可以解释C++代码.你希望这个程序在运行时理解C++代码吗?

#include <iostream>

int main()
{
    std::string line;
    while (std::getline(std::cin, line)) {
        std::cout << line << '\n';
    }
}
Run Code Online (Sandbox Code Playgroud)