按照教程中的代码 hello.js文件
function Hello() { var name; this.setName = function( thyName ){ name = thyName; }; this.sayHello = function(){ console.log( 'Hello ' + name ); }; } module.exports = Hello;
然后是getmodule.js文件
var myModule = require( './hello' ); myModule.setName( 'BYVoid' ); myModule.sayHello();
第一行require到一个空的对像,里面并没有setName和sayHello方法 所以会报错说TypeError:Object #(object)has not method 'setName'
如果用exports.Hello = Hello; 在getmodule.js中会得到一个对像,对像里有一个元素是Hello对像,但Hello对像也是空的
刚学nodejs,google上也搜不到相关的贴子,请大神们帮解惑,敬谢