Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export objects vs functions NodeJs

Export Objects {} vs Export function

I'm developing an exercise application, I came across to the question When do I need to exports an object {} instead of a function class?

Scenario example:

I'm building a simple authentication module using the object style.

// file auth.js
module.exports = {

    login: function() {
       // code login
    },

    logout: function() {
      // code logout
    },

    register: function() {
      // code register
    }
}

Here I'm using the anonymous function style

module.exports = function() {

        return {
            login: function() {
               // code login
            },

            logout: function() {
              // code logout
            },

            register: function() {
              // code register
            }
         }
    }

Requiring

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

It will work with both the approaches, but I'm confused to choose which fit better and why.

Questions

  • How do you understand in your design of a module, when is appropriate to exports, an object, anonymous function, named function to Instantiate?

  • Which are the difference and how the require method behave, when requiring these functions or Objects ?

like image 621
Fabrizio Fenoglio Avatar asked Dec 20 '14 14:12

Fabrizio Fenoglio


People also ask

What is advantage of using exports or module exports in a node JS application?

The main purpose of module. exports is to achieve modular programming. Modular programming refers to separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.

What is export function in node JS?

exports. The module. exports is a special object which is included in every JavaScript file in the Node. js application by default. The module is a variable that represents the current module, and exports is an object that will be exposed as a module.

What is the difference between module export and export?

exports and module. exports both point to the same object. exports is a variable and module. exports is an attribute of the module object.

Can we export function in node?

Node. js treats each file in a Node project as a module that can export values and functions from the file.


2 Answers

How do you understand in your design of a module, when is appropriate to exports, an object, anonymous function, named function to Instantiate?

  • true minimalists aim to export a single function if that will suffice. This is based on the scope of your module and assumes a small and focused purpose.
  • Export an object of related functions when you need a set of functions to have a meaningful set of functionality. For example, a tempConversion module might have both cToF and fToC functions.
  • If you are doing OO, exporting the constructor function is fine.
  • a closure function that returns the real payload (which can be an object or a function) is useful for configuration options. For example var tip = require('computeTip')(18); could store 18 in closure and return a function that would calculate 18% 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's redundant, so just export the average function directly to make it more concise. If you have more than one function you want to expose, use an object whose properties are the functions.

Which are the difference and how the require method behave, when requiring these functions or Objects ?

  • There are no differences. require has a single behavior that accommodates each of these techniques.
like image 180
Peter Lyons Avatar answered Oct 19 '22 04:10

Peter Lyons


In both your examples you actually return an object and not a function class. In the second case you get the object by executing the function. In order to use the function as a class you should return a function which acts as the constructor, and then instantiate with the new operator.

That being said, I would prefer an object over a class if I consider my module as a singleton, something that would be common to every file that would include it. An example would be a module that implements a common access layer to my database.

A class makes more sense if you intent to instantiate it multiple times. An example would be a specific model class for your database schema, like a User class.

like image 3
fxenik Avatar answered Oct 19 '22 04:10

fxenik