While it is always best practice to never extend the prototype
of Object, it is fine to extend Object directly with your own static methods. There are 4 methods that I find critical to working with JavaScript objects: forEach, getIgnoreKeys, is, and toQueryString. The forEach
method iterates on the Object using the "for … in" loop, but calls getIgnoreKeys
to determine which keys are methods attached to the Object prototype, and ignores those values. You can also use the forEach
method to iterate on an Array, even though it would be more efficient to use the forEach
method we attached to Array in my last post. Lastly, the is
Function is simply a type detection method and toQueryString
calls forEach
so that we can construct a query string, "key1=value1&key2=value2&key3=value3…".
Check out object.js here and the unit test here.