简体   繁体   中英

Is module.exports = { fn } same as exports.fn = fn

For job test i need to create library like this:

// do-it.js
function doIt(smth) {}

module.exports = {
    doIt,
};

But i'm working with typescript and compiled do-it.ts file looks like this:

// do-it.js when compiled from do-it.ts
exports.__esModule = true;
exports.doIt= exports.another = void 0;

function doIt(smth) {}
exports.doIt = doIt;

function another() {}
exports.another= another;

Is exports from this two examples will work the same?

Roughly speaking, yes; certainly the resulting exports are the same.

I mean, if you did:

module.exports = {
    doIt,
};

and later did

module.exports = {
    doSomethingElse,
};

you'd have a problem, because the second one completesly replaces the previous exports object.

You don't need to create that object at all, it's created for you before your module is called. So really, you can just do

exports.doIt = doIt;

and then

exports.doSomethingElse = doSomethingElse;

in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM