Javascript 导出和模块之间有什么区别。在ES中导出
有时我会看到: 导出默认类 及 module.exports= 有什么区别?我们将在哪种情况下使用哪一种?只是为了澄清一下。这是node.js特有的 在节点中创建模块就像将其包装在下面的函数中一样。实际上,这或多或少就是node正在做的事情Javascript 导出和模块之间有什么区别。在ES中导出,javascript,node.js,Javascript,Node.js,有时我会看到: 导出默认类 及 module.exports= 有什么区别?我们将在哪种情况下使用哪一种?只是为了澄清一下。这是node.js特有的 在节点中创建模块就像将其包装在下面的函数中一样。实际上,这或多或少就是node正在做的事情 function (exports, require, module, __filename, __dirname) { } 因此,exports变量实际上是对module.exports的引用,就像这样,可以有两个变量指向同一个对象。导出和module
function (exports, require, module, __filename, __dirname) {
}
因此,exports变量实际上是对module.exports的引用,就像这样,可以有两个变量指向同一个对象。导出和module.exports引用到内存中的同一点。在使用导出时必须稍微小心一点,因为有一些“暗”部分与javascript对象连接,并且有一些模式可能会中断。以下是一个例子:
dependency_exports.js
exports = function () {
console.log('dependency');
}
// Place console.log to see the contents of the exports and module.exports
console.log(exports);
console.log(module.exports);
app.js
var dependency = require('./dependency_exports');
dependency();
如果依赖项_exports.js文件中的两个console.log指向同一内存,为什么它们会打印不同的对象?请记住,exports是对module.exports的引用。
当我们将另一个值赋给该变量(导出)时,我们会中断引用,并在内存中创建一个新的点。
它不会更新module.exports内的引用
所以现在如果我们尝试这样做:
dependency();
由于此问题,我们将看到返回错误。尚未看到答案