将整个模块拉入当前作用域Node.js [英] Pull Entire Module into current scope Node.js

查看:105
本文介绍了将整个模块拉入当前作用域Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,如果我的模块看起来像这样:

p>

  exports.chai = requirechai
exports.mocha = requiremocha
exports.Q = requireq

我如何访问单个变量,就像它们是当前文件中的全局变量?



感谢任何建议

解决方案

使用coffeescript,我建议您使用解构分配功能:

  {chai,mocha,Q} = require'some_module'

//使用chai,mocha和Q好东西


I'm trying to pull an entire module into the current scope of another file after a require call.

For instance if my module looks like this:

exports.chai = require "chai"
exports.mocha = require "mocha"
exports.Q = require "q"

How can I then access the individual variables like they are global variables in the current file? So instead of doing something like requireVar.chai I can just type chai?

Thanks for any advice

解决方案

Using coffeescript, I recommend you use the destructuring assignment feature:

{chai, mocha, Q} = require 'some_module'

// use chai, mocha and Q for good stuff

这篇关于将整个模块拉入当前作用域Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆