我正在尝试创建一个主对象,该对象将保存我的程序的数据,如下所示:
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));
它返回' undefined。'
不确定原因。
答案 0 :(得分:2)
您的函数manufacturingInfo
没有return
某些东西,因此调用的值是 undefined ,但它确实对state
进行了更改,所以也许你真的很想
init();
manufacturingInfo(10, 500, 30);
console.log(state);
答案 1 :(得分:0)
该功能不会返回任何内容。它实际上是成功运行该功能。但是因为你没有return语句,返回值将是undefined
。
要更改它,请在函数中添加return
语句。
答案 2 :(得分:0)
您希望它返回什么?
manufacturingInfo = function(price, containers, servings) {
state.runInfo.price = price;
state.runInfo.containers = containers;
state.runInfo.servings = servings;
return state.runInfo; // anything here you want the function to report
};