<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # module-exports ## 比較一下module.exports 和exports.xxx module.exports才是真正的接口,exports只不過是它的一個輔助工具。 最終返回給調用的是module.exports而不是exports。 所有的exports收集到的屬性和方法,都賦值給了module.exports。 當然,這有個前提,就是Module.exports本身不具備任何屬性和方法。如果module.exports已經具備一些屬性和方法,那么exports收集來的信息將被忽略 ### 共存的噩夢 修改exports.js如下: ~~~ module.exports = 'Exports IT!'; exports.name = function () { console.log('hello world'); } ~~~ 再次引用執行e_main.js ~~~ var hello = require('./exports'); console.log(hello.name()) ~~~ 發現報錯:對象hello沒有name方法 exports模塊忽略了exports收集的name方法,返回了一個字符串“Exports IT!”。 由此可知,你的模塊并不一定非得返回“實例化對象”。 你的模塊可以是任何合法的javascript對象--boolean, number, date, JSON, string, function, array等等。 你的模塊可以是任何你設置給它的東西。如果你沒有顯式的給module.exports設置任何屬性和方法,那么你的模塊就是exports設置給module.exports的屬性。 ### string ~~~ exports.name = "StuQ微課堂" ~~~ ### boolean 布爾值就是真或者假,在js表示為true或false ~~~ exports.is_valid = true ~~~ ### number 數值有很多種,大家記得它是描述數字相關的即可 常見如整形,浮點型 年齡 ~~~ exports.age = 20 ~~~ 圓周率 ~~~ var pi = 3.1415; exports.PI = pi ~~~ ### date 日期類型 ~~~ var date = new Date() exports.created_at = date; ~~~ 那么怎么樣獲取2015-11-20呢? 使用[momentjs](http://momentjs.com/docs/) 首先需要安裝momentjs ~~~ npm install --save moment ~~~ date.js實例代碼 ~~~ var moment = require('moment'); var date = moment().format('YYYY-MM-DD'); exports.created_at = date; ~~~ 測試main.js ~~~ var date = require('./date'); console.log(date.created_at); ~~~ 執行結果如下 ~~~ ? nodejs-practice git:(master) ? node nodejs/demo/date/main.js 2015-11-20 ~~~ ### JSON json.js ~~~ exports.person = { name: "西瓜", age:18 } ~~~ 創建main.js ~~~ var json = require('./json') console.log(json.person.name); // console.log(json.persion); for(var k in json.person){ console.log("key = " + k + " && value = " + json.person[k]); } ~~~ ### 最簡單的面向對象 下面例子中,你的模塊是一個類exports2.js: ~~~ module.exports = function(name, age) { this.name = name; this.age = age; this.about = function() { console.log(this.name +' is '+ this.age +' years old'); }; }; ~~~ 可以這樣應用它: ~~~ var Person = require('./exports2'); var p = new Person('Ting', 20); p.about(); // Ting is 20 years old ~~~ ### 數組 下面例子中,你的模塊是一個數組: ~~~ module.exports = ['eric', '海角', '西瓜', '李小賤', 'kevin']; ~~~ 可以這樣應用它: ~~~ var arr = require('./exports3.js'); console.log('第3個是西瓜: ' + arr[2]); console.log('-------------------\n'); for(var i = 0; i < arr.length; i++){ console.log("第" + i + "個是 " + arr[i]); } console.log('-------------------\n'); for(var i = 1; i < arr.length; i++){ console.log("第" + i + "個是 " + arr[i]); } console.log('-------------------\n'); var i = 0; arr.forEach(function(name){ console.log("第" + i + "個是 " + name); i++; }) ~~~ 現在你應該明白了,如果你想你的模塊是一個特定的類型就用module.exports。 如果你想的模塊是一個典型的“實例化對象”就用exports。 給Module.exports添加屬性類似于給exports添加屬性。例如: ~~~ module.exports.name = function() { console.log('My name is Lemmy Kilmister'); }; ~~~ 同樣,exports是這樣的 ~~~ exports.name = function() { console.log('My name is Lemmy Kilmister'); }; ~~~ 請注意,這兩種結果并不相同。 前面已經提到module.exports是真正的接口,exports只不過是它的輔助工具。 推薦使用exports導出,除非你打算從原來的“實例化對象”改變成一個類型 ## dependency cycle循環引用 a.js ~~~ console.log('a starting'); exports.done = false; var b = require('./b.js'); console.log('in a, b.done = %j', b.done); exports.done = true; console.log('a done'); ~~~ b.js ~~~ console.log('b starting'); exports.done = false; var a = require('./a.js'); console.log('in b, a.done = %j', a.done); exports.done = true; console.log('b done'); ~~~ main.js ~~~ console.log('main starting'); var a = require('./a.js'); var b = require('./b.js'); console.log('in main, a.done=%j, b.done=%j', a.done, b.done); ~~~ 當main.js加載a.js的時候,a.js加載b.js,同時,b.js想要加載a.js,這時候就產生了依賴閉環的問題,為了避免無限循環,需要打破這個閉環。根據CommonJS Modules/1.0規范中的說明「在這種情況下,”require”返回的對象必須至少包含此外部模塊在調用require函數(會進入當前模塊執行環境)之前就已經準備完畢的輸出。」,有些繞,讓我們從依賴閉環產生的地方跟蹤,b.js需要require a.js,這里b.js做為當前模塊,a.js相對于b.js來說是外部模塊,那么a.js的輸出應該是在其require b.js之前(即「進入當前模塊執行環境」)就應該返回,執行過程如下: a.js ~~~ console.log('a starting'); exports.done = false; // 只執行到這里,然后exports返回給調用模塊(b.js),以下被丟棄 var b = require('./b.js'); console.log('in a, b.done = %j', b.done); exports.done = true; console.log('a done'); ~~~ 然后b.js繼續執行完成。以下是執行結果: ~~~ $ node main.js main starting a starting b starting in b, a.done = false b done in a, b.done = true a done in main, a.done=true, b.done=true ~~~ 注意,雖然main.js同時require了a.js和b.js,但是根據node.js的模塊緩存策略,模塊只執行一次。
                  <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>

                              哎呀哎呀视频在线观看