Export NodeJs Objects and Functions

Export Objects {} vs Export Function

I am developing an exercise application, I came across a question When do I need to export an object {} instead of a function class?

Example script:

I am creating a simple authentication module using the style of an object.

 // file auth.js module.exports = { login: function() { // code login }, logout: function() { // code logout }, register: function() { // code register } } 

Here I use an anonymous function style

 module.exports = function() { return { login: function() { // code login }, logout: function() { // code logout }, register: function() { // code register } } } 

Demanding

When I want to require this module, I just do:

 var auth = require('auth'); auth.login(); // trigger login function via object auth().login() // trigger login function via function 

He will work with both approaches, but I am embarrassed to choose which one is better and why.

Questions

  • As you understand in your module design, when it is suitable for export, an object , an anonymous function , with the name of the function to create

  • What is the difference and how does the require method behave when these functions or Objects are required?

+7
javascript function object
source share
2 answers

How do you understand a module in its design when it is suitable for export, an object, an anonymous function, a named function for activation?

  • True minimalists tend to export one function, if that is enough. This is based on the size of your module and involves a small and focused goal.
  • Export an object of related functions if you need a set of functions in order to have a meaningful set of functions. For example, the tempConversion module may have cToF and fToC .
  • If you are doing OO, exporting a constructor function is great.
  • A close function that returns the actual payload (which may be an object or function) is useful for configuration parameters. For example, var tip = require('computeTip')(18); can store 18 in closure and return a function that would calculate 18% of the tip when called with a number.

Here's a rule of thumb : if you export only one named function a la require('average').average(listOfNumbers) , it is redundant, so just export the average function to make it more concise. If you have several functions that you want to open, use an object whose properties are functions.

What is the difference and how does the require method behave when these functions or objects are required?

  • There are no differences. require has one behavior that each of these methods supports.
+4
source share

In both examples, you are actually returning an object, not a function class. In the second case, you get the object by performing a function. To use a function as a class, you must return a function that acts as a constructor, and then create an instance with a new statement.

In this case, I would prefer an object over a class if I viewed my module as a singleton, which would be common to every file that will include it. An example is a module that implements a general level of access to my database.

A class makes sense if you intend to create it several times. An example would be a specific model class for your database schema, such as a user class.

+2
source share

All Articles