我是 Node 新手。我正在尝试使用 .notation 访问对象属性和方法,但它给出了对象属性的 undefined
错误,以及对象的 "TypeError: dice.roll is not a function "
功能。
这里是代码:
目标文件:
var dice = {
size: 4,
count:3,
roll:function(){
var result = Math.ceil(this.size * Math.random());
return result;
}
};
exports.diceObjectName = dice;
程序文件:
var dice = require("./dice");
console.log(dice);
console.log(dice.roll());
最佳答案
您需要使用module.exports = dice
var dice = {
size: 4,
count:3,
roll:function(){
var result = Math.ceil(this.size * Math.random());
return result;
}
};
module.exports = dice;
关于javascript - 访问 JavaScript 对象属性和函数时出现未定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48263864/