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() {
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?
Fabrizio
source share