Node.js global variables

前端 未结 6 1521
耶瑟儿~
耶瑟儿~ 2020-11-22 13:06

I asked here: Does Node.js require inheritance?

And I was told that I can set variables to the global scope by leaving out the variable.

This does not

6条回答
  •  [愿得一人]
    2020-11-22 13:30

    Use a global namespace like global.MYAPI = {}:

    global.MYAPI._ = require('underscore')
    

    All other posters talk about the bad pattern involved. So leaving that discussion aside, the best way to have a variable defined globally (OP's question) is through namespaces.

    Tip: Development Using Namespaces

提交回复
热议问题