我想将前两个函数中的两个对象添加在一起,并在另一个函数中将添加的对象一起返回。现在我该怎么办?
function firstfunc(){
var object1 = {
//Relevant code
};
}
function secondfunc(){
var object2 = {
//Relevant code
};
}
function thirdfunc(){
var total = Object.assign(object1, object2);
return total;
}
答案 0 :(得分:3)
返回前两个函数中的对象并在第三个函数中调用这些函数
function firstfunc(){
var o = {
foo:'bar'
};
return o;
}
function secondfunc(){
var o = {
bar:'foo'
};
return o;
}
function thirdfunc(){
var total = Object.assign({}, firstfunc(), secondfunc());
return total;
}
console.log(thirdfunc())
答案 1 :(得分:0)
现在,object1和object2的作用域为firstfunc和secondfunc,因此无法访问thirdfunc。您是否想将firstfunc和secondfunc的结果合并在一起?看起来像这样:
function firstfunc() {
return {
// object 1 data
};
}
function secondfunc() {
return {
// object 2 data
};
}
function thirdfunc(){
return Object.assign({}, firstfunc(), secondfunc());
}