简体   繁体   English

module.exports = { fn } 是否与exports.fn = fn 相同

[英]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:但我正在使用 typescript 和编译的 do-it.ts 文件如下所示:

// 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.你会遇到问题,因为第二个完全替换了之前的exports object。

You don't need to create that object at all, it's created for you before your module is called.您根本不需要创建 object,它是在调用模块之前为您创建的。 So really, you can just do所以真的,你可以做

exports.doIt = doIt;

and then接着

exports.doSomethingElse = doSomethingElse;

in the first place.首先。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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