久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      nodejs模塊是什么

      模塊是Node.js應(yīng)用程序的基本組成部分,文件和模塊是一一對應(yīng)的,一個Nodejs模塊就是一個文件,而這個文件可能是JavaScript代碼、JSON或者編譯過的“C/C++”擴展,引用模塊可用“require('文件路徑')”語句。

      nodejs模塊是什么

      本教程操作環(huán)境:windows7系統(tǒng)、nodejs 12.19.0版、Dell G3電腦。

      為了讓Node.js的文件可以相互調(diào)用,Node.js提供了一個簡單的模塊系統(tǒng)。

      模塊是Node.js 應(yīng)用程序的基本組成部分,文件和模塊是一一對應(yīng)的。換言之,一個 Node.js 文件就是一個模塊,這個文件可能是JavaScript 代碼、JSON 或者編譯過的C/C++ 擴展。

      對于nodejs來說,一個文件就是一個模塊,你可以export接口出去,也可以require別的模塊進來。

      // module1.js exports.func1 = function(){         console.log('func1 from module1 called'); }

      module1把函數(shù)func1通過exports對象作為模塊的公共訪問接口。

      //module2.js var in_module1 = require('./module1.js'); in_module1.func1(); exports.func2 = function(){         console.log('func2 from module2 called'); }

      module2把module1 require進來,這個時候,in_module1就相當(dāng)于module1的exports對象。當(dāng)使用in_module1調(diào)用func1的時候,相當(dāng)于通過module1的exports對象調(diào)用func1。

      同時,module2自己的函數(shù)func2也通過模塊的exports對象作為module2公共接口。

      // module3.js var in_module2 = require('./module2.js'); in_module2.func2();

      同理,module3把module2 require進來,此時in_module2就相當(dāng)于module2的exports對象。

      運行結(jié)果如下:

      rlan@rlan-LA:~/nodejs/nodetest$ node module2.js func1 from module1 called rlan@rlan-LA:~/nodejs/nodetest$ node module3.js func1 from module1 called func2 from module2 called

      nodejs引入模塊不僅僅得到模塊的公共接口,同時會把文件里別的語句一并引用進來,比如:

      module1.js改為

      // module2.js console.log('this is in module2'); var in_module1 = require('./module1.js');    in_module1.func1();    exports.func2 = function(){        console.log('func2 from module2 called'); }

      module2引入了module1的func1函數(shù),同時執(zhí)行了module1中的打印語句:

      rlan@rlan-LA:~/nodejs/nodetest$ node module1.js this is in module1 rlan@rlan-LA:~/nodejs/nodetest$ node module2.js this is in module2         - module2 self this is in module1         - require module1 func1 from module1 called  - module2 self

      現(xiàn)在,module2 載入了module1,module3載入了module2,如果module3再載入一次module1會怎么樣呢?

      // module3.js var in_module1 = require('./module1.js'); var in_module2 = require('./module2.js');  in_module1.func1(); in_module2.func2();

      這時候,module3首先載入了module1,又載入了module2,module2自己又載入了module1的部分,運行結(jié)果為

      rlan@rlan-LA:~/nodejs/nodetest$ node module3.js this is in module1        -  require module1 this is in module2        -  require module2 func1 from module1 called -  require module2 func1 from module1 called -  module3 self func2 from module2 called -  module3 self

      假如把module3的require順序調(diào)整一下:

      // module3.js var in_module2 = require('./module2.js'); var in_module1 = require('./module1.js');  in_module1.func1(); in_module2.func2();

      運行結(jié)果為:

      rlan@rlan-LA:~/nodejs/nodetest$ node module3.js this is in module2         - require module2 this is in module1         - require module2 func1 from module1 called  - require module2 func1 from module1 called  - module3 self func2 from module2 called  - module3 self

      看起來nodejs用某種機制保證了同一個模塊在另一個模塊里不會被重復(fù)載入,所以

      this is in module1

      這一行只出現(xiàn)了一次,雖然在module3.js里似乎被載入了兩次。

      那么,如果循環(huán)載入了會發(fā)生什么呢?現(xiàn)在我們讓module1來require module2:

      // module1.js console.log('this is in module1');  var in_module2 = require('./module2.js');  exports.func1 = function(){         console.log('func1 from module1 called'); }
      // module2.js console.log('this is in module2'); var in_module1 = require('./module1.js');  in_module1.func1();  exports.func2 = function(){         console.log('func2 from module2 called'); }

      運行結(jié)果如下:

      rlan@rlan-LA:~/nodejs/nodetest$ node module1.js this is in module1 this is in module2 /home/rlan/nodejs/nodetest/module2.js:4 in_module1.func1();            ^ TypeError: in_module1.func1 is not a function     at Object.<anonymous> (/home/rlan/nodejs/nodetest/module2.js:4:12)     at Module._compile (module.js:410:26)     at Object.Module._extensions..js (module.js:417:10)     at Module.load (module.js:344:32)     at Function.Module._load (module.js:301:12)     at Module.require (module.js:354:17)     at require (internal/module.js:12:17)     at Object.<anonymous> (/home/rlan/nodejs/nodetest/module1.js:3:18)     at Module._compile (module.js:410:26)     at Object.Module._extensions..js (module.js:417:10) rlan@rlan-LA:~/nodejs/nodetest$ node module2.js this is in module2 this is in module1 func1 from module1 called

      nodejs似乎阻止了載入自己的行為,運行module2的時候,行為跟module1沒有載入module2的結(jié)果一樣,并沒有報錯。而在運行module1的時候,當(dāng)走到module2里面,忽略了require module1的語句之后,module2調(diào)用了module1的func1,程序出錯。

      綜上,nodejs里嵌套重復(fù)載入模塊(或者載入自己)的require語句是不能正確執(zhí)行的。

      【推薦學(xué)習(xí):《nodejs 教程》】

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號