<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # commonJS ## commonJS規范和hello world2 ### commonJS是一個規范 commonJS是一個規范,講起來內容會有非常多,我們來點直接的要點 CommonJS對模塊的定義十分簡單,主要分為模塊引用、模塊定義和模塊標識3個部分。 核心如下 * require - 用來引入依賴 * export - 用來導出模塊,包括標識符(identifier)和模塊內容(contents) * module.exports * exports.xxx 下面以helloworld2來演示具體的用法 ### 關于 helloworld2 helloworld2會有2個文件,用來演示多文件直接引用關系 * helloworld2.js * main.js ### 創建`helloworld2.js` ~~~ module.exports = function () { console.log('hello world'); } ~~~ 這里使用`module.exports`導出一個`function`,里面只是打印了`hello world` 下面我們看一下如何在main.js里調用 ### 創建`main.js` ~~~ var hello = require('./helloworld2'); hello() ~~~ 這樣需要說明的是,通過require來引用helloworld2這個模塊,一旦你require了這個模塊,那么這個模塊對外暴露的方法或變量,你就可以調用了 再回想一下`module.exports`的作用:當前module暴露的方法或變量 那么此處我們可能會有的疑問 * require引用的是一個本地文件(注意./helloworld2代表的是當前目錄下的helloworld2.js),如果是其他庫呢? * module.exports這里只是暴露了一個function,那如果想暴露更多方法或變量呢? ### 執行`main.js` ~~~ ? nodejs git:(master) ? node demo/main.js hello world ~~~ ### 我們如果再變一下呢? helloworld2這個module導出的是一個function,既然是function那么就一定可以直接調用 創建`main.js` ~~~ require('./helloworld2')(); ~~~ 執行 ~~~ ? nodejs git:(master) ? node demo/main2.js hello world ~~~ ### 那么我如果想跟某人打招呼呢? 創建`helloworld3.js` ~~~ module.exports = function (person) { console.log('hello world ' + person); } ~~~ 創建`main3.js` ~~~ require('./helloworld3')('海角'); ~~~ 執行 ~~~ ? nodejs git:(master) ? node demo/main3.js hello world 海角 ~~~ ### 我想調用獨立函數 創建`helloworld4.js` ~~~ function say(person) { console.log('hello world ' + person); } module.exports = say; ~~~ 創建`main4.js` ~~~ require('./helloworld3')('海角'); ~~~ 執行 ~~~ ? nodejs git:(master) ? node demo/main4.js hello world 海角 ~~~ ## 解答疑問 ### require引用的是一個本地文件,如果是其他庫呢? (注意./helloworld2代表的是當前目錄下的helloworld2.js) ### module.exports這里只是暴露了一個function,那如果想暴露更多方法或變量呢? 我們來假設一下,在helloworld5里既能吃飯又能打招呼呢? 也就是說這個模塊要提供2個方法 * 吃飯 * 打招呼 創建`helloworld5.js` ~~~ function say(person) { console.log('i am say hello world to ' + person); } function eat(food) { console.log('i am eat ' + food); } exports.eat = eat; exports.say = say; ~~~ 創建`main5.js` ~~~ var h5 = require('./helloworld5') h5.eat('蘭州拉面'); h5.say('海角'); ~~~ 執行 ~~~ ? nodejs git:(master) ? node demo/main5.js i am eat 蘭州拉面 i am say hello world to 海角 ~~~ 如果你還記得之前的模塊是如何導出,你就會對比一下 之前用的是module.exports,現在用的是exports.xxx 那么你好奇,它們有什么差別么?
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看