require():使用module.exports直接分配给“this”
我想知道在使用这两种方法时是否有任何优点或缺点:
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
(或只是exports
)是标准的CommonJS方式。
在Node.js中, this
恰好是相同的对象,但是最好不要依赖this
对象,并且使用它将不能与其他工具一起工作,例如,Browserify