2020年10月27日 星期二

重新踏入網頁開發 (2) - ES6

模組化

    Ryan Dahl 創造的 node.js 專案之所以如此重要, 是因為其理念為 模組(module), 讓 javascript 的大規模專案得以實現, 也讓 javascript 可以做伺服器端程式設計。

node.js 的模組

    這裡示範 node.js 原生的模組,
  • moduleA.js
        // 想像這裡有隱藏程式碼
        // # var module = new Module(...);
        // # var exports = module.exports;
        // index.js 在 call "require("./ModuleA");"
        // 等於回傳 module.exports
        var name = "JunYe";
        exports.name = name;
  • moduleB.js
        // 想像這裡有隱藏程式碼
        // # var module = new Module(...);
        // # var exports = module.exports;
        // index.js 在 call "require("./ModuleB");"
        // 等於回傳 module.exports
        var name = "Daniel";
        exports.name = name;
  • index.js
        var a = require("./moduleA");
        var b = require("./moduleB");
        console.log(a.name + " and " + b.name + " both are the best.");
  • 執行 node index.js
        $>:node index.js 
        JunYe and Daniel both are the best.

ES6 的模組

    ES6 是較新的 javascript 規範, 沒錯就是規範, javascript 並非正規的程式語言, 而是一種語言規範, 而其 runtime 的執行則讓各個瀏覽器引擎決定(nodejs也是其中之一)。雖然 ES6 是較新的規範, 但大部分的瀏覽器都有支援了。以下為 nodejs 支援 ES6 的示範
  • studentA.js
        export var _name = "JunYe";
        export var _class = "SSS";
        export var _number = "3064";
  • function.js
        export function intro (name, number) {
            console.log("My name is " + name + ", and my number is " + number);
        };
  • index.js
        // 要在 package.json 加 type: module
        // 才能達到 <script src="./function.js" type="module"></script> 的效果
        // 這邊不知道為什麼要加 .js 才給過
        import { intro } from "./function.js";
        import { _name, _number } from "./studentA.js";
        intro(_name, _number);
  • 執行 node index.js
        $>:node index.js 
        My name is JunYe, and my number is 3064
上一篇 :
下一篇 :
參考資料 :

Related Posts:

  • 重新踏入網頁開發 (1) - Nodejs 前言     網頁開發是一個對我而言還蠻陌生的東西, 因為我都是自學所以總是得過且過。自學通常就會拿手邊資源也就是公司內部系統的 Code。公司的 Code 較為古早, 不外乎 3 個基本的元素, http server(apache), PHP, 前端(html, css, javascript)。 此時的我靠著 w3schools 就能完成所有事情。既然重新踏入開發網頁, 就去學最新的東西 (目標是 react), 即使… Read More
  • 重新踏入網頁開發 (2) - ES6 模組化     Ryan Dahl 創造的 node.js 專案之所以如此重要, 是因為其理念為 模組(module), 讓 javascript 的大規模專案得以實現, 也讓 javascript 可以做伺服器端程式設計。 node.js 的模組     這裡示範 node.js 原生的模組, moduleA.js // 想像這裡有隱藏程式碼 // # var modul… Read More
  • 重新踏入網頁開發 (6) - Express - 5  Express - sendFile()     用 sendFile 把寫好的 HTML 傳出去,然後準備開始前端的開發。 import express from 'express' import path from 'path'; // 建立 express 這 module var app = express() const port = 8888 cons… Read More
  • 重新踏入網頁開發 (5) - Blocking & Non-Blocking Blocking & Non-Blocking     Nodejs 為事件觸發的單一執行緒,不像 Apache + PHP 會自動開一個 Thread 去接每一個 Request。     Blocking Code 示範 (程式碼另有 index.js 請參照重新踏入網頁開發 (4) - Dependency injection) route.js function start_… Read More
  • 重新踏入網頁開發 (7) - React  React - Introduction     A JavaScript library for building user interfaces. React 在 MVC 分類上屬於 View,也就是主要用來開發前端。比起直接使用 npx create-react-app,我這裡想紀錄一些較簡單且原始的 React Sample Code。 原本既有的程式碼 <!-- Some HT… Read More

0 意見:

張貼留言

Popular Posts