我想知道在使用这两种方法时是否存在任何利弊:
first.js:
- this.myFunction = function() {
- return 'herro first';
- }
second.js:
- module.exports = obj = {};
- obj.myFunction = function() {
- return 'herro second';
- }
然后将上面两个包括在内并用作如下:
app.js:
- var first = require('./first.js');
- console.log(first.myFunction());
- var second = require('./second');
- console.log(second.myFunction());
解决方法
module.exports(或只是导出)是标准的CommonJS方式.
在Node.js中,这恰好是同一个对象,但最好不要依赖它,并且使用它不能与其他工具一起使用,例如Browserify