Implementing Object.assign in JavaScript
Object.assign() is a fundamental JavaScript method for copying the values of all enumerable own properties from one or more source objects to a target object. Implementing this functionality yourself provides a deeper understanding of how object properties are handled and copied in JavaScript. This challenge asks you to recreate this method, ensuring it handles various scenarios correctly.
Problem Description
Your task is to implement a function named assign that mimics the behavior of Object.assign(). The function should take a target object as the first argument, followed by one or more source objects. It should copy all enumerable own properties from the source objects to the target object, in the order they are provided. If a property exists on both the target and a source object, the source object's value should overwrite the target object's value. The function should return the modified target object.
Key Requirements:
- Shallow Copy: The copy should be shallow. This means that if a property value is an object or array, only the reference to that object/array is copied, not a deep copy of its contents.
- Enumerable Own Properties: Only enumerable own properties (properties directly on the object, not inherited from its prototype chain) should be copied.
- Order Matters: Properties should be copied in the order of the source objects provided. Later source objects should overwrite properties from earlier source objects if there are conflicts.
- Target Object Modification: The target object should be modified directly and returned.
- Multiple Source Objects: The function should accept any number of source objects.
Expected Behavior:
The function should behave identically to Object.assign() in terms of property copying and overwriting.
Edge Cases to Consider:
- Null or Undefined Target: If the target object is
nullorundefined, it should be converted to an empty object before copying properties. - Null or Undefined Source Objects: Null or undefined source objects should be ignored.
- Non-Object Sources: If a source object is not an object (e.g., a number, string, boolean), it should be converted to an object before copying properties.
- Symbols: Symbols should not be copied.
Object.assigndoes not copy symbol properties.
Examples
Example 1:
Input: assign({a: 1, b: 2}, {b: 3, c: 4})
Output: {a: 1, b: 3, c: 4}
Explanation: The target object initially has {a: 1, b: 2}. The first source object {b: 3, c: 4} overwrites 'b' and adds 'c'. The final result is {a: 1, b: 3, c: 4}.
Example 2:
Input: assign({}, {a: 1}, {b: 2}, {a: 3, c: 4})
Output: {a: 3, b: 2, c: 4}
Explanation: The target object starts as {}. The first source object {a: 1} adds 'a'. The second source object {b: 2} adds 'b'. The third source object {a: 3, c: 4} overwrites 'a' and adds 'c'. The final result is {a: 3, b: 2, c: 4}.
Example 3:
Input: assign({a: 1}, null, {b: 2}, undefined, {a: 3})
Output: {a: 3, b: 2}
Explanation: Null and undefined source objects are ignored. The target object starts as {a: 1}. The first source object (null) is skipped. The second source object {b: 2} adds 'b'. The third source object (undefined) is skipped. The fourth source object {a: 3} overwrites 'a'. The final result is {a: 3, b: 2}.
Constraints
- The function
assignmust accept at least one argument (the target object). - The function
assigncan accept any number of source objects. - The target object can be any JavaScript object, or
nullorundefined. - Source objects can be any JavaScript object, or
null,undefined, or primitive values. - The function must return the modified target object.
- The implementation should be reasonably efficient (avoid unnecessary iterations or complex operations).
Notes
- Consider using a
for...argsloop to iterate over the source objects. - Remember to handle the edge cases mentioned above.
- The
Object.keys()method can be helpful for iterating over an object's properties. - Think about how to handle the case where the target object is initially
nullorundefined. - Focus on clarity and correctness. While performance is a consideration, prioritize a working and understandable solution.