详细说明均以写在注释之中,这里就不啰嗦了,小伙伴们自己详细看吧,千万别吧注释当成空气了。
/*在node中,可以使用require()函数来加载模块. * require函数使用一个参数,参数值可以带有完整路径的模块的文件名,也可以为模块名.当使用node中提供的模块时,在require函数中只需要指定模块名即可. * */ //建立一个页面2.js;代码如下 var name="思思博士"; exports.name=name; //建立一个页面1.js;代码如下 var two=require("./2.js"); console.log(two.name); //输出结果:思思博士 /* * 在node中所有的脚本文件都是一个模块文件,因此1.js也是一个模块文件,又由于该文件是在命令行窗口中通过node命令被直接运行的,因此在node中该模块文件被定义为应用程序的主模块 * 可以用如下的方法检测出当前的模块是否是主模块 * */ if(module===require.main){ console.log("当前模块时主模块"); } //输出结果:当前模块时主模块 //2.js代码 var name="思思博士"; console.log(name); exports.name=name; //1.js代码: var two=require("./2.js"); var two=require("./2.js"); //虽然引用了2次,但是只是执行了1次console.log(name)的输出. /*require.resolve(str) * 在node中,可以使用这个函数来查询某个模块文件的带有完整绝对路径的文件名. * */ var url=require.resolve("./2"); console.log(url); //输出结果:E:\node\gys\2.js /*require.cache * 在node中,这个属性代表了所有已被加载模块的缓存区. * */ var two=require("./2.js"); var cache=require.cache; console.log(cache); /*输出结果: * { 'E:\\node\\gys\\1.js': { id: '.', exports: {}, parent: null, filename: 'E:\\node\\gys\\1.js', loaded: false, children: [ [Object] ], paths: [ 'E:\\node\\gys\\node_modules', 'E:\\node\\node_modules', 'E:\\node_modules' ] }, 'E:\\node\\gys\\2.js': { id: 'E:\\node\\gys\\2.js', exports: { name: '思思博士' }, parent: { id: '.', exports: {}, parent: null, filename: 'E:\\node\\gys\\1.js', loaded: false, children: [Object], paths: [Object] }, filename: 'E:\\node\\gys\\2.js', loaded: true, children: [], paths: [ 'E:\\node\\gys\\node_modules', 'E:\\node\\node_modules', 'E:\\node_modules' ] } } * */ //2.js代码 var name="思思博士"; console.log(name); //1.js代码 //当使用delete关键字删除缓存区中缓存的某个模块对象后,下次加载该模块时将重新运行该模块中的代码.使用代码: var two=require("./2.js"); var two1=require("./2.js"); console.log("删除前") delete require.cache[require.resolve("./2.js")]; console.log("删除后"); var two2=require("./2.js"); /* * 输出结果: * 思思博士 * 删除前 * 删除后 * 思思博士 * */
童鞋们是否了解了node中require()函数的用法了呢,本文是自己的一些理解,如有遗漏,还请大家指正。
更多node.js使用require()函数加载模块相关文章请关注PHP中文网!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
