简体   繁体   English

Expressjs JavaScript Fundamentals:exports = module.exports = createApplication;

[英]Expressjs JavaScript Fundamentals: exports = module.exports = createApplication;

I don't know what this pattern is called, if I did I would look it up directly. 我不知道这个模式叫什么,如果我这样做,我会直接查找。

Mainly, how does this work? 主要是,这是如何工作的? (This code is taken from Express.js) (此代码取自Express.js)

exports = module.exports = createApplication;

I have seen similar patterns before where there is this type of reference variable chain ex: 在有这种类型的引用变量链ex之前我见过类似的模式:

x = y = z

I understand exports vs module.exports but looking at the pattern above it makes me question on how it actually works. 我理解export vs module.exports,但是看看上面的模式让我对它的实际工作方式提出质疑。

I go by the general rule of thumb that 'module.exports' is the real deal, and 'exports' is its helper, more on this here 我按照一般的经验法则说'module.exports'是真正的交易,'exports'是它的帮手,更多关于这里

Is the module pattern like this (without altering module.exports)? 模块模式是这样的(不改变module.exports)吗?

exports = module.exports = {};

ex: 例如:

exports.name = 'hello' 

results to 结果

exports = module.exports = {name: 'hello'}

What happens when you change the reference on exports? 更改出口参考时会发生什么?

exports = {name: 'bob'}

Now when you add to exports, it will reference the `{name: 'bob'} and no longer have any ties with module.exports? 现在当你添加到导出时,它将引用`{name:'bob'}并且不再与module.exports有任何联系?

Your intuition is correct. 你的直觉是正确的。 I'll work from the bottom up: 我会自下而上地工作:

The Node.js Wrapper Node.js Wrapper

Before running any file, Node.js wraps the entire script in an immediately-invoked function expression (IIFE) : 在运行任何文件之前,Node.js 将整个脚本包装在一个立即调用的函数表达式(IIFE)中

(function (exports, require, module, __filename, __dirname) {
    // ...script goes here...
});

This is how it introduces the module and exports variables into the scope; 这就是它如何引入module并将变量exports到范围中; they're nothing more special than function arguments. 它们并不比函数参数更特殊。

Using exports 使用exports

The Node.js docs on module.exports and the exports alias are pretty helpful. module.exports上的Node.js文档和exports别名非常有用。 To start out, module.exports and the exports variable both refer to the same empty object that was created by the module system. 首先, module.exportsexports变量都引用模块系统创建的同一个空对象。

If a module simply needs to export a plain old JavaScript object with some properties set, exports is all that's needed: 如果一个模块只需要一个普通的旧JavaScript对象导出一些属性进行设置, exports是所有的需要:

exports.get = function(key) {
    // ...
};

exports.set = function(key, value) {
    // ...
};

When code require() s this module, the result will look something like: 当代码require() s这个模块时,结果将类似于:

{
    get: [Function],
    set: [Function]
}

Replacing module.exports 替换module.exports

However, Express exports a constructor function, createApplication , as the root value. 但是,Express createApplication构造函数createApplication作为根值createApplication To export the function itself, rather than just assigning it to a property on the exported object, it must first replace the exported object entirely: 要导出函数本身,而不是仅将其分配给导出对象上的属性,它必须首先完全替换导出的对象:

module.exports = createApplication;

Now, exports hasn't been updated and still refers to the old object, whereas module.exports is a reference to createApplication . 现在, exports尚未更新,仍然引用旧对象,而module.exports是对createApplication的引用。 But if you keep reading , you'll notice that Express exports several other properties in addition to the constructor. 但是如果你继续阅读 ,你会发现除了构造函数之外,Express还会导出其他几个属性。 While it would be sufficient to assign these to properties on the new value of module.exports , it is shorter to reassign exports so that it is again an alias to module.exports , then assign those properties on exports , which is equivalent to assigning them on module.exports . 虽然将这些属性分配给module.exports的新值就module.exports ,但是重新分配exports以使它再次成为module.exports的别名,然后在exports上分配这些属性,这相当于分配它们。在module.exports

Thus, these examples are functionally equivalent: 因此,这些示例在功能上是等效的:

module.exports = createApplication;

function createApplication() {
    // ...
}

module.exports.application = proto;
module.exports.request = req;
module.exports.response = res;

But this one is less verbose: 但这个不那么冗长:

exports = module.exports = createApplication;

function createApplication() {
    // ...
}

exports.application = proto;
exports.request = req;
exports.response = res;

Either way, the result is the same, a function named createApplication at the root with properties available on it: 无论哪种方式,结果都是相同的,在根上名为createApplication的函数,其上有可用的属性:

{
    [Function: createApplication]
    application: [Object],
    request: [Object],
    response: [Object],
    // ...a few other properties...
}

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

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