简体   繁体   English

在node.js中处理`exports`的建议

[英]Suggestions for dealing with `exports` in node.js

Theory: 理论:

One of the things that appeals to me about node.js is using it as a command line tool. 关于node.js吸引我的一件事是将它用作命令行工具。

In theory, I can write libraries in Javascript and place them in my ~/.node_libraries directory, and then then I can reuse those libraries. 从理论上讲,我可以用Javascript编写库并将它们放在我的~/.node_libraries目录中,然后我可以重用这些库。

So for instance, I have a text.js in ~/.node_libraries , and it has a bunch of text-related functions I use over and over ( depunctuate() , tokenize_text() , things like that). 所以,例如,我在~/.node_libraries有一个~/.node_libraries ,它有一堆我反复使用的文本相关函数( depunctuate()tokenize_text() ,类似的东西)。

The beauty of this is that I could use the same text.js file with my command-line scripts and server side. 这样做的text.js是我可以在命令行脚本和服务器端使用相同的text.js文件。 Right now I'm doing all that text processing stuff with Python, but I'd like to just stick to one language. 现在我正在使用Python进行所有文本处理工作,但我只想坚持使用一种语言。

Practice: 实践:

AFAICT, in order to create a node.js module, I have to attach everything that I want to be available to exports or this . AFAICT,为了创建node.js模块,我必须附加我想要exports所有东西或者this Ie, in text.js , I have to do: 即,在text.js ,我必须这样做:

exports.depunctuate = depunctuate

or 要么

this.depunctuate = depunctuate

If I use exports , I have problems with using the library server-side à la: 如果我使用exports ,我在使用库服务器端àla时遇到问题:

<script src=text.js></script>

because then I get exports is not defined errors. 因为那时我得到的导出没有定义错误。

If I use this , I avoid the error, but everything I export ends up getting attached to the window object. 如果我使用this ,我避免错误,但我导出的所有内容最终都会附加到窗口对象。

Is there some way I can set up these libraries that avoid both of these problems? 有什么方法可以设置这些库来避免这两个问题吗? For instance, is there some way I can wrap the exporting of exports so that the var will be apparent to node, but not when it's used in a plain Javascript file on a server? 例如,有一些方法可以让我换的出口exports ,以使VAR将是显而易见的节点,而不是当它在一个普通的服务器上的JavaScript文件的使用?

How about testing for the existence of the exports object before sticking stuff in it? 如何在粘贴东西之前测试exports对象的存在?

This has worked well for me so far, but maybe there are better ideas out there: 到目前为止,这对我来说效果很好,但也许有更好的想法:

if(typeof(exports) !== 'undefined' && exports !== null) {
  exports.foo = foo;
  exports.bar = bar;
}

In CoffeeScript, this can be done a little more tersely: 在CoffeeScript中,这可以更简洁地完成:

[exports.foo, exports.bar] = [foo, bar] if exports?

so this comes into a namespacing issue. 所以这就成了一个命名空间问题。 Unless a function is called with the new operator you will get a this context === to window (global). 除非使用new运算符调用函数,否则将获得此上下文===到窗口(全局)。 A way to dodge this is: 一种躲闪的方法是:

(function( exports ) {
  /* put your depuncuate definition here to keep it from leaking to the global */
  exports.depunctuate = depunctuate;
})( (typeof exports === 'undefined') ? myAppNamespace : exports );

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

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