[CH2] Node != JavaScript
本筆記非原創筆記,內容整理自 Advanced Node.js @ PluralSight。
Node's Architecture: V8 and libuv
node -v; // 查看 node 版本
V8 的功能可以分成 shipping, staged, 和 In Progress。
node -p 'process.versions.v8' // 查看使用的 v8 版本
node --harmony -p "<command>" // 可以使用 staged 的功能
node --v8-options | less // 可以看到所有的 v8 options

V8: 在 Node 中使用透過 V8 的 C++ API 使用 Chrome V8 引擎。Core Modules: Node 也提供我們可以直接使用 JavaScript 的 API,透過它我們可以處理檔案、網際網路等等。C++ Bindings: Node API 最終會透過 V8 的物件和函式樣版來執行 C++ 的程式碼。libuv: Node 用來處理非同步事件的函式庫(主要是 C 語言)。http-parser: 用來處 理 HTTP 訊息的 C 函式庫。c-ares: 執行非同步的 DNS queries。OpenSSLzlib: 處理非同步和串流的壓縮與解壓縮。
Node's ClI and REPL
Node Command
在 terminal 中輸入 node 即可進入 REPL(Read, Eval, Print, Loop)。
$ node # 進入 REPL
$ node --help | less # 顯示 node 可用的 options

$ node -p "<script>|<file>" # 執行 script 並列印
$ node -c
Node REPL
AutoComplete
在 REPL 中可以經常使用 TAB 鍵就可以查看這個物件底下有哪些方法或屬性可以使用(autocomplete):
$ let arr = []
$ arr.<TAB> # 列出所有 array 可用的方法
$ arr.s<TAB> # 列出所有 array 中開頭為 s 的方法
$ arr.sh<TAB> # 自動輸入 arr.shift

. 是在 REPL 中可以使用的關鍵字:
$ .<TAB>
$ .help<ENTER>

$ .exit # 離開 REPL
$ .save session.js # 儲存 REPL 的訊息
$ .load hello.js # 將 JS 檔載入 REPL 中
$ .editor # 進入 editor mode,如果要輸入多行可用
process.argv # 取的使用者輸入 node 後的參數