Salman
Salman

Reputation: 9447

module.exports or this.func() { }?

In NodeJS, if we define functions using this keyword, it gets exposed.

for example:

// module.js

this.func1 = function () {
    console.log('func1');
}

Then, If you require('module') you can access func1.

I want to know that how it is different than module.exports?

Thanks

Upvotes: 0

Views: 100

Answers (1)

freakish
freakish

Reputation: 56477

Simple test: create new file and do:

console.log( this );
console.log( module.exports );

this.test = 1;

console.log( this );
console.log( module.exports );

which clearly shows that this is a reference to module.exports, i.e. there is no difference.

Upvotes: 2

Related Questions