Object.assign
Sunny-117 opened this issue · comments
Sunny commented
Object.assign2 = function(target, ...source) {
let ret = Object(target)
source.forEach(function(obj) {
if (obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
ret[key] = obj[key]
}
}
}
})
return ret
}
const obj1 = {a: 1}
const obj2 = {b: 2}
const res = Object.assign2(obj1, obj2)
console.log(obj1);
kangkang123269 commented
function myAssign(target, ...sources) {
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
const to = Object(target);
sources.forEach(source => {
if (source != null) {
Object.keys(source).forEach(key => {
to[key] = source[key];
});
}
});
return to;
}