我正在尝试创建一个主对象来保存我的程序的数据,如下所示:
var state;
var init = function() {
state = {
runInfo: { //object that'll hold manufacturing info - run, desired price, servings/container
price: null,
containers: null,
servings: null
},
formula: [],
totalsServing: [],
totalsBottle: [],
totalsRun: []
};
};
我正在尝试使用以下函数在状态对象中设置 runInfo 对象的属性:
manufacturingInfo = function(price, containers, servings) {
state.runInfo.price = price;
state.runInfo.containers = containers;
state.runInfo.servings = servings;
};
当我像这样测试函数时:
init();
console.log(manufacturingInfo(10, 500, 30));
它返回“未定义”。
不知道为什么。
最佳答案
您的函数 manufacturingInfo
不会返回
某些内容,因此调用的值是未定义,但它确实会对 进行更改状态
,所以也许你真的想
init();
manufacturingInfo(10, 500, 30);
console.log(state);
关于javascript - 定义返回 'undefined' 的对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28813490/