简体   繁体   English

如何将参数传递给自执行的匿名函数

[英]How to pass a parameter to a self executing anonymous function

I have in my server.js this line of code: 我在server.js中有以下代码行:

 require('../routes/allRoutes')(app)

And this works fine when my allRoutes.js looks like this: 当我的allRoutes.js看起来像这样时,这很好用:

 module.exports = function(app){
    app.get("/", function(req, res){
      res.render ......
    });
 }

But what if my allRoutes.js looks like this: 但是,如果我的allRoutes.js看起来像这样:

  (function(allRoutes){

     app.get("/", function(req, res){
        res.render .....
      });
  })(module.exports)

How do I pass the app object in the anonymous, self executing function? 如何在匿名,自执行函数中传递应用程序对象?

Nevermind, I figured it out: 没关系,我想通了:

1. server.js 1. server.js

 require('./routes/allRoutes').init(app); 

2. allRoutes.js 2. allRoutes.js

 (function(allRoutes) { allRoutes.init = function(app) { app.get("/", function (req, res) { res.send('Hello You'); }) }; })(module.exports); 

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

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