Hone logo
Hone
Problems

Implement Object.assign in JavaScript

This challenge asks you to replicate the functionality of JavaScript's built-in Object.assign() method. This method is crucial for merging properties from one or more source objects into a target object, making it a fundamental tool for object manipulation, cloning, and default value setting.

Problem Description

Your task is to create a function named myAssign that accepts a target object as its first argument, followed by one or more source objects. The function should copy all enumerable own properties from each source object to the target object. The original target object should be mutated and returned.

Key Requirements:

  • The function must accept an arbitrary number of source objects.
  • It should iterate through each source object provided.
  • For each source object, it must copy all of its enumerable own properties to the target object.
  • If a property exists in multiple source objects, the property from the later source object in the argument list will overwrite the property from the earlier one.
  • The function should return the modified target object.

Expected Behavior:

  • Properties from source objects should be directly assigned to the target object.
  • The function should handle primitive values correctly (e.g., strings, numbers, booleans) as property values.
  • The function should handle null and undefined as property values.

Edge Cases to Consider:

  • What happens if no source objects are provided?
  • What happens if a source object is null or undefined?
  • What happens if the target object is null or undefined? (Note: The native Object.assign throws a TypeError in this case. You should replicate this behavior).
  • Handling properties with values like null or undefined.

Examples

Example 1: Merging properties from multiple sources.

const target = { a: 1, b: 2 };
const source1 = { b: 3, c: 4 };
const source2 = { c: 5, d: 6 };

const result = myAssign(target, source1, source2);
// result will be: { a: 1, b: 3, c: 5, d: 6 }
// target will be mutated to: { a: 1, b: 3, c: 5, d: 6 }

Explanation: b from source1 overwrites b in target. c from source2 overwrites c from source1. d from source2 is added.

Example 2: Handling null and undefined sources.

const target = { x: 10 };
const source1 = null;
const source2 = { y: 20 };
const source3 = undefined;
const source4 = { z: 30 };

const result = myAssign(target, source1, source2, source3, source4);
// result will be: { x: 10, y: 20, z: 30 }
// target will be mutated to: { x: 10, y: 20, z: 30 }

Explanation: null and undefined source objects are skipped without error.

Example 3: Overwriting with different types and handling missing properties.

const target = { name: "Alice", age: 30 };
const source1 = { age: "thirty", city: "New York" };

const result = myAssign(target, source1);
// result will be: { name: "Alice", age: "thirty", city: "New York" }
// target will be mutated to: { name: "Alice", age: "thirty", city: "New York" }

Explanation: The string value for age from source1 overwrites the number value in target. city is added.

Example 4: Target is null.

try {
  myAssign(null, { a: 1 });
} catch (e) {
  // e will be a TypeError
}

Explanation: Attempting to assign properties to null should throw a TypeError.

Constraints

  • The function signature must be function myAssign(target, ...sources).
  • You cannot use the built-in Object.assign() method or any methods that directly replicate its behavior (e.g., spread syntax for object merging).
  • You must iterate through the properties of the source objects using a method that accesses enumerable own properties (e.g., for...in loop with hasOwnProperty check, or Object.keys()).
  • The implementation should be efficient and avoid unnecessary operations.

Notes

  • Remember that for...in loops iterate over enumerable properties, including those inherited from the prototype chain. You will need to use Object.prototype.hasOwnProperty.call(source, key) to ensure you are only copying own properties.
  • Consider how JavaScript handles property assignment: when you assign a property to an object, it either creates a new property or overwrites an existing one.
  • The native Object.assign returns the target object. Your myAssign function must do the same.
Loading editor...
javascript