使用函数(对象)访问时如何解决未定义的变量?

时间:2019-03-22 09:54:32

标签: javascript node.js

我正在学习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 =未定义未定义年龄未定义

我该如何解决?

0 个答案:

没有答案
相关问题