Simple and clear way to reach this, im modern JS without iterate the prototypes, is like this:
Object.prototype.iterateProperties = ((callback) => { Object.keys(obj).filter(key => obj.hasOwnProperty(key)).forEach((key) => { callback(key, obj[key]); });});
Explain
This code creates a function in the prototype of all object - means function that is accessible in every Object
instance.The function iterate all own properties of the object and run a callback function which gets (key, value)
for each propery in the object.
Example for use
obj.iterateProperties((key, value) => console.log(key +': '+ value));