如何在不使用任何库的情况下在javascript函数中添加两个单词?

时间:2016-10-11 20:30:13

标签: javascript

如何通过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")

1 个答案:

答案 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)