1.commonjs Ä£¿é¹ÜÀí
¡¡¡¡ËùÓдúÂë¶¼ÔËÐÐÔÚÄ£¿é×÷ÓÃÓò£¬²»»áÎÛȾȫ¾Ö×÷ÓÃÓò¡£
¡¡¡¡Ä£¿é¿ÉÒÔ¶à´Î¼ÓÔØ£¬µ«ÊÇÖ»»áÔÚµÚÒ»´Î¼ÓÔØÊ±ÔËÐÐÒ»´Î£¬È»ºóÔËÐнá¹û¾Í±»»º´æÁË£¬ÒÔºóÔÙ¼ÓÔØ£¬¾ÍÖ±½Ó¶ÁÈ¡»º´æ½á¹û¡£ÒªÏëÈÃÄ£¿éÔÙ´ÎÔËÐУ¬±ØÐëÇå³ý»º´æ¡£
¡¡¡¡Ä£¿é¼ÓÔØµÄ˳Ðò£¬°´ÕÕÆäÔÚ´úÂëÖгöÏÖµÄ˳Ðò¡£
2.module¶ÔÏó
2.1 module.exportsÊôÐÔ
¡¡¡¡module.exportsÊôÐÔ±íʾµ±Ç°Ä£¿é¶ÔÍâÊä³öµÄ½Ó¿Ú£¬ÆäËûÎļþ¼ÓÔØ¸ÃÄ£¿é£¬Êµ¼ÊÉϾÍÊǶÁÈ¡module.exports±äÁ¿¡£
2.2 exports±äÁ¿
¡¡¡¡ÎªÁË·½±ã£¬NodeΪÿ¸öÄ£¿éÌṩһ¸öexports±äÁ¿£¬Ö¸Ïòmodule.exports¡£ÕâµÈͬÔÚÿ¸öÄ£¿éÍ·²¿£¬ÓÐÒ»ÐÐÕâÑùµÄÃüÁ
¡¡¡¡
var exports = module.exports;£¨commonJSÒþʽ×öÁËÕâ¸ö¸³Öµ£©
2.3 jsÎļþ¾ÍÊÇÒ»¸öº¯Êý
console.log(arguments){ ¡®0¡®: {}, ?¡®1¡®: ???{ [Function: require] ????resolve: { [Function: resolve] paths: [Function: paths] }, ????main: ??????Module { ???????id: ¡®.¡®, ???????exports: {}, ???????parent: null, ???????filename: ¡®C:\\Users\\liang\\Desktop\\node\\hello.js¡®, ???????loaded: false, ???????children: [], ???????paths: [Array] }, ????extensions: { ¡®.js¡®: [Function], ¡®.json¡®: [Function], ¡®.node¡®: [Function] }, ????cache: { ¡®C:\Users\liang\Desktop\node\hello.js¡®: [Object] } }, ?¡®2¡®: ???Module { ????id: ¡®.¡®, ????exports: {}, ????parent: null, ????filename: ¡®C:\\Users\\liang\\Desktop\\node\\hello.js¡®, ????loaded: false, ????children: [], ????paths: ??????[ ¡®C:\\Users\\liang\\Desktop\\node\\node_modules¡®, ???????¡®C:\\Users\\liang\\Desktop\\node_modules¡®, ???????¡®C:\\Users\\liang\\node_modules¡®, ???????¡®C:\\Users\\node_modules¡®, ???????¡®C:\\node_modules¡® ] }, ?¡®3¡®: ¡®C:\\Users\\liang\\Desktop\\node\\hello.js¡®, ?¡®4¡®: ¡®C:\\Users\\liang\\Desktop\\node¡® }
console.log(arguments.callee)
[Function]
console.log(arguments.length)
5
¡¡¡¡(function (exports, require, module, __filename, __dirname){})()
¡¡¡¡¡¡¡¡exportsÊÇmodule.exportsµÄÒ»¸öÒýÓÃ
¡¡¡¡¡¡¡¡requireÒýÓÃÄ£¿éºó£¬·µ»Ø¸øµ÷ÓÃÕßµÄÊÇmodule.exports¶ø²»ÊÇexports
¡¡¡¡¡¡¡¡exports.xxx£¬Ï൱ÓÚÔÚµ¼³ö¶ÔÏóÉϹÒÊôÐÔ£¬¸ÃÊôÐÔ¶Ôµ÷ÓÃÄ£¿éÖ±½Ó¿É¼û
¡¡¡¡¡¡¡¡exports =Ï൱ÓÚ¸øexports¶ÔÏóÖØÐ¸³Öµ£¬µ÷ÓÃÄ£¿é²»ÄÜ·ÃÎÊexports¶ÔÏó¼°ÆäÊôÐÔ
¡¡¡¡¡¡¡¡Èç¹û´ËÄ£¿éÊÇÒ»¸öÀ࣬¾ÍÓ¦¸ÃÖ±½Ó¸³Öµmodule.exports£¬ÕâÑùµ÷ÓÃÕß¾ÍÊÇÒ»¸öÀ๹ÔìÆ÷£¬¿ÉÒÔÖ±½ÓnewʵÀý
¡¡¡¡¡¡¡¡ÕâÊÇÁ½ÖÖ±©Â©µÄ·½Ê½,¶¼¿ÉÒÔʹÓÃ
ÏÂÃæÊÇÒýÈ뷽ʽ£¬¼°½á¹û
¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡ÎÒÃÇ¿ÉÒÔ¿´³É exports = module.exports = {} ¾ÍÊÇexports Ö¸ÕëÖ¸Ïòmodule.exportsµÄ¶ÔÏó
¡¡¡¡¡¡¡¡¹ØÓÚrfequire ÒýÈëµÄ·µ»ØÖµÊÇmodule.exports
¡¡¡¡¡¡¡¡
¡¡2.4 require ¹æÔò
¡¡¡¡£¨1£©Èç¹û²ÎÊý×Ö·û´®ÒÔ¡°/¡±¿ªÍ·£¬Ôò±íʾ¼ÓÔØµÄÊÇÒ»¸öλÓÚ¾ø¶Ô·¾¶µÄÄ£¿éÎļþ¡£±ÈÈ磬require(¡®/home/marco/foo.js¡®)½«¼ÓÔØ/home/marco/foo.js¡£
¡¡¡¡£¨2£©Èç¹û²ÎÊý×Ö·û´®ÒÔ¡°./¡±¿ªÍ·£¬Ôò±íʾ¼ÓÔØµÄÊÇÒ»¸öλÓÚÏà¶Ô·¾¶£¨¸úµ±Ç°Ö´Ðнű¾µÄλÖÃÏà±È£©µÄÄ£¿éÎļþ¡£±ÈÈ磬require(¡®./circle¡®)½«¼ÓÔØµ±Ç°½Å±¾Í¬Ò»Ä¿Â¼µÄcircle.js¡£
¡¡¡¡£¨3£©Èç¹û²ÎÊý×Ö·û´®²»ÒÔ¡°./¡°»ò¡±/¡°¿ªÍ·£¬Ôò±íʾ¼ÓÔØµÄÊÇÒ»¸öĬÈÏÌṩµÄºËÐÄÄ£¿é£¨Î»ÓÚNodeµÄϵͳ°²×°Ä¿Â¼ÖУ©£¬»òÕßÒ»¸öλÓÚ¸÷¼¶node_modulesĿ¼µÄÒѰ²×°Ä£¿é£¨È«¾Ö°²×°»ò¾Ö²¿°²×°£©
¡¡¡¡£¨4£©Èç¹û²ÎÊý×Ö·û´®²»ÒÔ¡°./¡°»ò¡±/¡°¿ªÍ·£¬¶øÇÒÊÇÒ»¸ö·¾¶£¬±ÈÈçrequire(¡®example-module/path/to/file¡®)£¬Ôò½«ÏÈÕÒµ½example-moduleµÄλÖã¬È»ºóÔÙÒÔËüΪ²ÎÊý£¬ÕÒµ½ºóÐøÂ·¾¶¡£
¡¡¡¡£¨5£©Èç¹ûÖ¸¶¨µÄÄ£¿éÎļþûÓз¢ÏÖ£¬Node»á³¢ÊÔΪÎļþÃûÌí¼Ó.js¡¢.json¡¢.nodeºó£¬ÔÙÈ¥ËÑË÷¡£.js¼þ»áÒÔÎı¾¸ñʽµÄJavaScript½Å±¾Îļþ½âÎö£¬.jsonÎļþ»áÒÔJSON¸ñʽµÄÎı¾Îļþ½âÎö£¬.nodeÎļþ»áÒÔ±àÒëºóµÄ¶þ½øÖÆÎļþ½âÎö¡£
¡¡¡¡£¨6£©Èç¹ûÏëµÃµ½requireÃüÁî¼ÓÔØµÄÈ·ÇÐÎļþÃû£¬Ê¹ÓÃrequire.resolve()·½·¨¡£
¡¡¡¡2.5Ä£¿é»º´æ
¡¡¡¡µÚÒ»´Î¼ÓÔØÄ³¸öÄ£¿éʱ£¬Node»á»º´æ¸ÃÄ£¿é¡£ÒÔºóÔÙ¼ÓÔØ¸ÃÄ£¿é£¬¾ÍÖ±½Ó´Ó»º´æÈ¡³ö¸ÃÄ£¿éµÄmodule.exportsÊôÐÔ¡£
¡¡¡¡
require(¡®./example.js¡®);require(¡®./example.js¡®).message = "hello";require(¡®./example.js¡®).message// "hello"
¡¡¡¡ÉÏÃæ´úÂëÖУ¬Á¬ÐøÈý´ÎʹÓÃrequireÃüÁ¼ÓÔØÍ¬Ò»¸öÄ£¿é¡£µÚ¶þ´Î¼ÓÔØµÄʱºò£¬ÎªÊä³öµÄ¶ÔÏóÌí¼ÓÁËÒ»¸ömessageÊôÐÔ¡£µ«ÊǵÚÈý´Î¼ÓÔØµÄʱºò£¬Õâ¸ömessageÊôÐÔÒÀÈ»´æÔÚ£¬Õâ¾ÍÖ¤Ã÷requireÃüÁûÓÐÖØÐ¼ÓÔØÄ£¿éÎļþ£¬¶øÊÇÊä³öÁË»º´æ¡£
¡¡¡¡Èç¹ûÏëÒª¶à´ÎÖ´ÐÐij¸öÄ£¿é£¬¿ÉÒÔÈøÃÄ£¿éÊä³öÒ»¸öº¯Êý£¬È»ºóÿ´ÎrequireÕâ¸öÄ£¿éµÄʱºò£¬ÖØÐÂÖ´ÐÐÒ»ÏÂÊä³öµÄº¯Êý¡£
¡¡¡¡ËùÓлº´æµÄÄ£¿é±£´æÔÚrequire.cacheÖ®ÖУ¬Èç¹ûÏëɾ³ýÄ£¿éµÄ»º´æ£¬¿ÉÒÔÏñÏÂÃæÕâÑùд¡£
¡¡¡¡2.6require.main
require.main === module// true
node-ѧϰ֮·02 commonJSÄ£¿é
ÔÎĵØÖ·£ºhttps://www.cnblogs.com/liangfc/p/9751975.html