One could also use the in
operator instead of Object.hasOwnProperty().
One must ask: do inherited properties count? For simple objects (like the examples obj1
and obj2
) it seems to not matter, but for more complex objects it will matter...
If in
can be used, then it may provide better performance.
function extend(obj1, obj2) {
for (var key in obj2) {
if (!(key in obj1)) {
obj1[key] = obj2[key];
}
}
}
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
P.S.
###P.S. ItIt is a shame that Object.assign() doesn't have an option to not overwrite existing properties... if it did, then the extend function could simply utilize that function...
One could also use the in
operator instead of Object.hasOwnProperty().
One must ask: do inherited properties count? For simple objects (like the examples obj1
and obj2
) it seems to not matter, but for more complex objects it will matter...
If in
can be used, then it may provide better performance.
function extend(obj1, obj2) {
for (var key in obj2) {
if (!(key in obj1)) {
obj1[key] = obj2[key];
}
}
}
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
###P.S. It is a shame that Object.assign() doesn't have an option to not overwrite existing properties... if it did, then the extend function could simply utilize that function...
One could also use the in
operator instead of Object.hasOwnProperty().
One must ask: do inherited properties count? For simple objects (like the examples obj1
and obj2
) it seems to not matter, but for more complex objects it will matter...
If in
can be used, then it may provide better performance.
function extend(obj1, obj2) {
for (var key in obj2) {
if (!(key in obj1)) {
obj1[key] = obj2[key];
}
}
}
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
P.S.
It is a shame that Object.assign() doesn't have an option to not overwrite existing properties... if it did, then the extend function could simply utilize that function...
One could also use the in
operator instead of Object.hasOwnProperty().
One must ask: do inherited properties count? For simple objects (like the examples obj1
and obj2
) it seems to not matter, but for more complex objects it will matter...
If in
can be used, then it may provide better performance.
function extend(obj1, obj2) {
for (var key in obj2) {
if (!(key in obj1)) {
obj1[key] = obj2[key];
}
}
}
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
###P.S. It is a shame that Object.assign() doesn't have an option to not overwrite existing properties... if it did, then the extend function could simply utilize that function...
One could also use the in
operator instead of Object.hasOwnProperty().
One must ask: do inherited properties count? For simple objects (like the examples obj1
and obj2
) it seems to not matter, but for more complex objects it will matter...
If in
can be used, then it may provide better performance.
function extend(obj1, obj2) {
for (var key in obj2) {
if (!(key in obj1)) {
obj1[key] = obj2[key];
}
}
}
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
###P.S. It is a shame that Object.assign() doesn't have an option to not overwrite existing properties...
One could also use the in
operator instead of Object.hasOwnProperty().
One must ask: do inherited properties count? For simple objects (like the examples obj1
and obj2
) it seems to not matter, but for more complex objects it will matter...
If in
can be used, then it may provide better performance.
function extend(obj1, obj2) {
for (var key in obj2) {
if (!(key in obj1)) {
obj1[key] = obj2[key];
}
}
}
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
###P.S. It is a shame that Object.assign() doesn't have an option to not overwrite existing properties... if it did, then the extend function could simply utilize that function...
One could also use the in
operator instead of Object.hasOwnProperty().
One must ask: do inherited properties count? For simple objects (like the examples obj1
and obj2
) it seems to not matter, but for more complex objects it will matter...
If in
can be used, then it may provide better performance.
function extend(obj1, obj2) {
for (var key in obj2) {
if (!(key in obj1)) {
obj1[key] = obj2[key];
}
}
}
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
###P.S. It is a shame that Object.assign() doesn't have an option to not overwrite existing properties...
One could also use the in
operator instead of Object.hasOwnProperty().
One must ask: do inherited properties count? For simple objects (like the examples obj1
and obj2
) it seems to not matter, but for more complex objects it will matter...
If in
can be used, then it may provide better performance.
function extend(obj1, obj2) {
for (var key in obj2) {
if (!(key in obj1)) {
obj1[key] = obj2[key];
}
}
}
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
One could also use the in
operator instead of Object.hasOwnProperty().
One must ask: do inherited properties count? For simple objects (like the examples obj1
and obj2
) it seems to not matter, but for more complex objects it will matter...
If in
can be used, then it may provide better performance.
function extend(obj1, obj2) {
for (var key in obj2) {
if (!(key in obj1)) {
obj1[key] = obj2[key];
}
}
}
var obj1 = {
a: 1,
b: 2
};
var obj2 = {
b: 4,
c: 3
};
extend(obj1, obj2);
console.log(obj1); // --> {a: 1, b: 2, c: 3}
console.log(obj2); // --> {b: 4, c: 3}
###P.S. It is a shame that Object.assign() doesn't have an option to not overwrite existing properties...