Day 4: Polyfill for Object.keys()

Dharan Ganesan - Jul 13 '23 - - Dev Community

Today, we'll create a polyfill for the Object.keys() method. The Object.keys() method returns an array of a given object's own enumerable property names. Our goal is to create a function called myKeys that will implement this behavior.

// Test case
const obj = { name: "Bob", age: 1000 };
console.log(Object.myKeys(obj)); // Output: ["name", "age"]
Enter fullscreen mode Exit fullscreen mode

Plan

To solve this problem, we'll follow these steps:

  • Implement the myKeys function to retrieve the enumerable property names of the object.
  • Use a loop or a built-in method to iterate over the object's properties and collect the keys.
  • Return an array containing the collected keys.
  • Check if the Object.myKeys property exists.
  • If it doesn't exist, create a new property called myKeys on Object and assign it a function.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player