`

[Node.js] 学习笔记 - right design for your modules

阅读更多
http://fredkschott.com/post/2013/12/node-js-cookbook---designing-singletons/

Private vs. Public
When creating a new module, your variables and functions are kept private by default. You’ll need to use module.exports to define your interface and make them public. This is a strait-forward process: whatever you set to module.exports becomes public, and is visible when your module is required.

Your goal when designing modules should be to keep as much code private as possible, and only export what needs to be referenced by the rest of your application. Public functions can still reference private variables and functions, so there’s no harm in being conservative when choosing between the two.

You should be able to look in one place to see exactly what’s public, what’s private, and what the hell is going on. Kill two birds with one stone: define AND export your public variables at the same time. You’ll have an explicit separation of private and public as well as a complete picture of your public interface, instead of constantly searching your code base to find which functions are being exported.
// Private
var b = 2;
function sum(x, y) {
  return x + y;
}
function isXEqualTo5() {
  return (module.exports.x == 5);
}

// Public
module.exports = {
  x: 5,
  add2: function(num) {
      return sum(b, num);
  },
  addX: function(num) {
      return sum(module.exports.x, num);
  }
}
 


Avoiding the Exports Alias
A seasoned Node veteran will know there’s a second way to export your code. exports is an alias to module.exports, which you can use instead to save yourself a few characters. But be careful: it’s just an alias. Setting individual properties works fine, but if you set the entire exports or module.exports object (as we do above) you’ll break the connection between the two and it will no longer work.

If you have the same singleton code in two different files, Node will treat these as two separate modules. If they’re completely stateless this is fine, but if you’re not careful it can cause some major headaches down the road. Make sure you keep your code in a single place so that require can cache it correctly.

分享到:
评论

相关推荐

    Node.js(node-v16.15.1-linux-x64.tar.xz)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    Node.js(node-v16.15.1-linux-arm64.tar.xz)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    Node.js(node-v16.15.1-linux-armv7l.tar.xz)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    Node.js(node-v16.15.1-win-x86.zip)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    Node.js(node-v16.15.1.tar.gz 源码)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    Node.js(node-v16.15.1-x64.msi)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    node-v18.16.0-win-x64

    Node.js发布于2009年5月,由Ryan Dahl开发,是一个基于Chrome V8引擎的JavaScript运行环境,使用了一个事件驱动、非阻塞式I/O模型, [1] 让JavaScript 运行在服务端的开发平台,它让JavaScript成为与PHP、Python、...

    Node.js(node-v16.15.1-aix-ppc64.tar.gz)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    Node.js(node-v16.15.1-darwin-arm64.tar.gz)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    node.js 安装包 10.16.3-x64

    node.js 安装包 10.16.3-x64

    Node.js-iconv-lite-纯javascript转换字符编码

    iconv-lite - 纯javascript转换字符编码

    Node.js(node-v16.15.1-linux-ppc64le.tar.xz)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    Node.js(node-v16.15.1-linux-s390x.tar.xz)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    Node.js(node-v16.15.1-darwin-x64.tar.gz)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    Node.js Design Patterns Second Edition[July 2016]

    You will learn, for example, how different traditional design patterns look in Node.js, or how to design modules that do only one thing. A set of patterns to solve common Node.js design and coding ...

    node.js-for-windows-32位及64位

    。。。。。。。node.js-for-windows-32位及64位

    Node.js(node-v16.15.1-x86.msi)

    Node.js 应用程序是用 JavaScript 编写的,可以在 Mac OS X、Windows 和 Linux 上的 Node.js 运行时中运行而无需更改。 Node.js 应用程序旨在最大限度地提高吞吐量和效率,使用非阻塞 I/O 和异步事件。Node.js 应用...

    node-v16.19.0-win-x64.7z

    Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行时

    Node.js Design Patterns_Second Edition

    The book is meant for developers and software architects with a basic working knowledge of JavaScript who are interested in acquiring a deeper understanding of how to design and develop enterprise-...

    Node.js.for.Embedded.Systems

    By bringing accessibility to embedded components such as sensors and microcontrollers, JavaScript and Node.js might shape the world of physical computing as they did for web browsers. This practical ...

Global site tag (gtag.js) - Google Analytics