简体   繁体   English

什么是node.js模块?

[英]What are node.js modules?

Regarding this question: What is the purpose of Node.js module.exports and how do you use it? 关于此问题: Node.js module.exports的用途是什么,如何使用它?

I'm a Javascript beginner. 我是Java语言的初学者。 In the referenced question... 在引用的问题中...

mymodule.js code mymodule.js代码

var myFunc = function() { ... };
exports.myFunc = myFunc;

main js file js主文件

var m = require('./mymodule.js');
m.myFunc();

Is mymodule essentially a class file defining objects? mymodule本质上是一个定义对象的类文件吗?

Node.js allows code to be separated into different modules. Node.js允许将代码分为不同的模块。 This modules are just javascript files that can expose functions or objects using the exports object. 这些模块只是javascript文件,可以使用exports对象公开函数或对象。

  1. There are more details of this convention 此约定有更多详细信息
  2. Nice documentation of the Node.js modules 不错的Node.js模块文档

There are no Classes in JavaScript but you can use patterns to emulate that behaviour. JavaScript中没有类,但是您可以使用模式来模拟这种行为。 There is a question about implementing OOP patterns in JavaScript: what pattern to use when creating javascript class? 有一个关于在JavaScript中实现OOP模式的问题: 创建javascript类时要使用哪种模式?

As a beginner there are very good books for JavaScript: 作为初学者,有很多关于JavaScript的好书:

  1. JavaScript: The Good Parts JavaScript:好零件
  2. JavaScript Patterns JavaScript模式

They are short and will give you a very good insight of the JavaScript Language. 它们很简短,可以使您对JavaScript语言有很好的了解。

Is mymodule essentially a class file defining objects? mymodule本质上是一个定义对象的类文件吗?

and functions, although in Javascript functions are objects, so the distinction may be moot. 和函数,尽管在Javascript中函数对象,所以区别可能没有意义。

Importantly, each module has its own scope, so any var declared therein will not be visible outside of the module. 重要的是,每个模块都有自己的作用域,因此在其中声明的任何var在模块外部均不可见。

The rest of your question about users and lists doesn't make sense as written. 关于用户和列表的其余问题与书面说明没有任何意义。 Javascript OO programming is a complete topic in its own right, and the module system doesn't really change that. Java OO编程本身就是一个完整的主题,而模块系统并没有真正改变这一点。 Modules are just a way of wrapping code libraries. 模块只是包装代码库的一种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM