1
我有類似的問題,這樣的: Node JS - Passing Javascript object by reference to other files通行證參考需要的文件
接受的答案是需要我們每個要使用的文件的其他文件。
例如: 文件functions.js
exports.functionIWantToUse = function() { //code } ;
其他文件:
var functionsIWantToUse = require('./functions');
但現在,如果我們將需要這些文件中的所有其他文件,不會我們只是浪費內存? 這個文件將被加載multile時間正確嗎?如果我們將有10個大小爲1MB的文件需要多個文件?
我想在mainApp.js中需要這些文件,並且只需要一次。然後在其他文件中引用它。
爲了這一點,我的問題:
//database
var dbConfig = require('./dbConfig'),
var db = dbConfig.db;
//
//routes files
var config = require(modulesPath+'config'),
achivements = require(modulesPath+'achievements'),
frontendProcessor = require(modulesPath+'frontendProcessor'),
encryption = require(modulesPath+'encryption'),
userOperations = require(modulesPath+'user');
//
我想用數據庫中的其他文件,如: 成就,frontendProcessor,加密,USEROPERATIONS。 dbConfig.js中有一些代碼。不希望在每個文件中再次需要它,例如在其他問題中接受的答案。