index.js 555B

1234567891011121314151617181920212223
  1. var hasOwn = Object.prototype.hasOwnProperty;
  2. var toString = Object.prototype.toString;
  3. module.exports = function forEach (obj, fn, ctx) {
  4. if (toString.call(fn) !== '[object Function]') {
  5. throw new TypeError('iterator must be a function');
  6. }
  7. var l = obj.length;
  8. if (l === +l) {
  9. for (var i = 0; i < l; i++) {
  10. fn.call(ctx, obj[i], i, obj);
  11. }
  12. } else {
  13. for (var k in obj) {
  14. if (hasOwn.call(obj, k)) {
  15. fn.call(ctx, obj[k], k, obj);
  16. }
  17. }
  18. }
  19. };