我正在学习JS模块。我使用以下代码将customObject.js作为模块:
exports.customObject= {
firstName:"FName",
lastName:"LName",
publicationCount:2,
fullDetails: fullName(this)
};
function fullName(customObj) {
return customObj.firstName +' '+ customObj.lastName +' aged ' + customObj.publicationCount;
}
可以通过以下方式从hello.js中访问它:
var CustomObjectModule=require('./customObject')
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
showCustomObject(res)
}).listen(8080);
function showCustomObject(res){
res.write("<p>CustomObjectModule
lastName="+CustomObjectModule.customObject.fullDetails+'</P>');
}
运行时显示 CustomObjectModule lastName =未定义未定义年龄未定义
我该如何解决?