我可能有一个非常简单的问题,但我无法解决。我将发布一些由于某些原因而无法正常工作的代码片段:
//authentication_handler.js
var new_user = server.authenticated_users.add_user(new User(world));
//user.js
function User(world) {
personBody = createPlayerBody(100, 100, 8, 8, false, true, world);
exports.TheBody = personBody;
}
//player_data_handler.js
module.exports = function(server, client) {
var theUser = server.authenticated_users.find_user(client.uuid);
if (theUser != null) {
var playerData = JSON.stringify({
x: theUser.TheBody.position.x, //this line throws the error below
y: theUser.TheBody.position.y
});
server.authenticated_users.each_other(theUser, function(user) {
server.send_id_message(user.client, 5, playerData);
});
}
};
我得到的错误是这样的:
无法读取未定义的属性“位置”
我对JavaScript还是很陌生,我也不知道为什么“ personBody”变量没有一直传递到“ player_data_handler.js”。我尝试了更多的方法,但没有一个起作用。很感谢任何形式的帮助!!
最佳答案
exports
用于导出模块的各个部分。如果要向对象添加某些内容,请使用this
:
代替exports.TheBody = personBody;
尝试this.TheBody = personBody;