如何通过js函数添加2个对象而不使用' join'以下结果输出是真的吗?这是我现在的代码:
var twoObjs = function(obj1, obj2) {
return obj1 + obj2; // does not work
};
twoObjs("dog", "ball");
var output = mergeObjs({dog: "Max"}, {toy: "mouse"});
console.log(output.cat === "Max")
console.log(output.toy === "mouse")
答案 0 :(得分:2)
您可以使用Object.assign
创建新对象
var mergeObjs = function(obj1, obj2) {
return Object.assign({},obj1,obj2);
};
var output = mergeObjs({
dog: "Max"
}, {
toy: "mouse"
});
console.log(output.dog)
console.log(output.toy)