Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

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...

Update explanation
Source Link

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...

add postscript
Source Link

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...

update link
Source Link
Loading
Source Link
Loading
default

AltStyle によって変換されたページ (->オリジナル) /