DUET PG Score Card 2021
NHAI Recruitment 2021
Bihar Board 12th Dummy Admit Card 2022
RBI Assistant Notification 2021
Jee Advanced 2021 Result
NIACL Assistant Recruitment 2021
SBI PO Online Form 2021

NodeJS Exporting Functions Using ‘exports’: Methods of Exporting Constructs Using exports

Exporting Functions Using ‘exports’

const alpha = () => {
   // some code here
}

const beta = () => {
   // some code here
}

exports.alpha = alpha;
exports.beta = beta;

Feedback: Before the code in a module is evaluated, the exports object contains the value of the module.exports object. Initially, when the code is not evaluated, the default value of the module.exports object is an empty object. In other words, module.exports references an empty object. The default value (empty object) of module.exports is the one that is assigned to the exports object. In other words, the module.exports object is passed by reference to the exports object. Thus, exports object starts referencing the module.exports object and pointing to the empty object. The current status (before the evaluation of the code inside the module) is as follows:

module.exports = {}
exports = {} // since exports = module.exports

In this option, you are manipulating the exports object by adding some properties to it. Thus, the module.exports object will also be impacted and all the changes will be reflected in it too.

At the end, the status will be:

module.exports = { alpha: [Function: alpha], beta: [Function: beta] }
exports = { alpha: [Function: alpha], beta: [Function: beta] }

You also know at the end of a module, the module.exports object is returned. Since the module.exports object consists of both the functions, alpha and beta, this option is one of the correct choices.

exports.alpha = () => {
   // some code here
}

exports.beta = () => {
   // some code here
}

Feedback: Before the code in a module is evaluated, the exports object contains the value of the module.exports object. Initially, when the code is not evaluated, the default value of the module.exports object is an empty object. In other words, module.exports references an empty object. The default value (empty object) of module.exports is the one that is assigned to the exports object. In other words, the module.exports object is passed by reference to the exports object. Thus, the exports object starts referencing the module.exports object and pointing to the empty object. Thus, the current status (before evaluation of code inside the module) is as follows:

module.exports = {}
exports = {} // since exports = module.exports

In this option, you are manipulating the exports object by adding some properties to it. Thus, the module.exports object will also be impacted and all the changes will be reflected in it too. Notice that you are directly adding the functions as properties to the exports object while defining them.

At the end, the status will be:

module.exports = { alpha: [Function: alpha], beta: [Function: beta] }
exports = { alpha: [Function: alpha], beta: [Function: beta] }

You also know that at the end of a module, the module.exports object is returned. Since the module.exports object consists of both the functions, alpha and beta, this option is one of the correct choices.

const alpha = () => {
   console.log('alpha');
}

const beta = () => {
   console.log('beta');
}

exports = {
   alpha: alpha,
   beta: beta
}

module.exports = exports;

Feedback: Before the code in a module is evaluated, the exports object contains the value of the module.exports object. Initially, when the code is not evaluated, the default value of the module.exports object is an empty object. In other words, module.exports references an empty object. The default value (empty object) of module.exports is the one that is assigned to the exports object. In other words, the module.exports object is passed by reference to the exports object. Thus, the exports object starts referencing the module.exports object and pointing to the empty object. The current status (before the evaluation of the code inside the module) is as follows:

module.exports = {}
exports = {} // since exports = module.exports

In this option, you are assigning a new object to the exports variable using the object literal. This will make the exports variable point to a different object, whereas the module.exports will keep pointing to the original object, which is the empty object. Thus, the module.exports variable will not be impacted. 

The status will now become:

module.exports = { }
exports = { alpha: [Function: alpha], beta: [Function: beta] }

At the end, you are assigning the module.exports variable everything contained inside the exports variable. Thus, module.exports will start pointing to exports. The status will finally become:

module.exports = { alpha: [Function: alpha], beta: [Function: beta] }
exports = { alpha: [Function: alpha], beta: [Function: beta] }

You also know at the end of a module, module.exports object is returned. Since the module.exports object consists of both the functions, alpha and beta, this option is one of the correct choices.

Leave a Comment