|
| 1 | +// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys |
| 2 | +Object.keys = Object.keys || (function() { |
| 3 | + 'use strict'; |
| 4 | + var hasOwnProperty = Object.prototype.hasOwnProperty, |
| 5 | + hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'), |
| 6 | + dontEnums = [ |
| 7 | + 'toString', |
| 8 | + 'toLocaleString', |
| 9 | + 'valueOf', |
| 10 | + 'hasOwnProperty', |
| 11 | + 'isPrototypeOf', |
| 12 | + 'propertyIsEnumerable', |
| 13 | + 'constructor' |
| 14 | + ], |
| 15 | + dontEnumsLength = dontEnums.length; |
| 16 | + |
| 17 | + return function(obj) { |
| 18 | + if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) { |
| 19 | + throw new TypeError('Object.keys called on non-object'); |
| 20 | + } |
| 21 | + |
| 22 | + var result = [], prop, i; |
| 23 | + |
| 24 | + for (prop in obj) { |
| 25 | + if (hasOwnProperty.call(obj, prop)) { |
| 26 | + result.push(prop); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + if (hasDontEnumBug) { |
| 31 | + for (i = 0; i < dontEnumsLength; i++) { |
| 32 | + if (hasOwnProperty.call(obj, dontEnums[i])) { |
| 33 | + result.push(dontEnums[i]); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + return result; |
| 38 | + }; |
| 39 | +}()); |
| 40 | + |
| 41 | +// Reproducing steps of ECMA-262, Edition 5, 15.4.4.18 |
| 42 | +// Reference: http://es5.github.com/#x15.4.4.18 |
| 43 | +Array.prototype.forEach = Array.prototype.forEach || function( callback, thisArg ) { |
| 44 | + |
| 45 | + var T, k; |
| 46 | + |
| 47 | + if ( this === null || this === undefined ) { |
| 48 | + throw new TypeError( "this is null or not defined" ); |
| 49 | + } |
| 50 | + |
| 51 | + // 1. Let O be the result of calling ToObject passing the |this| value as the argument. |
| 52 | + var O = Object(this); |
| 53 | + |
| 54 | + // 2. Let lenValue be the result of calling the Get internal method of O with the argument |
| 55 | + // "length". |
| 56 | + // 3. Let len be ToUint32(lenValue). |
| 57 | + var len = O.length >>> 0; // Hack to convert O.length to a UInt32 |
| 58 | + |
| 59 | + // 4. If IsCallable(callback) is false, throw a TypeError exception. |
| 60 | + // See: http://es5.github.com/#x9.11 |
| 61 | + if ( {}.toString.call(callback) !== "[object Function]" ) { |
| 62 | + throw new TypeError( callback + " is not a function" ); |
| 63 | + } |
| 64 | + |
| 65 | + // 5. If thisArg was supplied, let T be thisArg; else let T be undefined. |
| 66 | + if ( thisArg ) { |
| 67 | + T = thisArg; |
| 68 | + } |
| 69 | + |
| 70 | + // 6. Let k be 0 |
| 71 | + k = 0; |
| 72 | + |
| 73 | + // 7. Repeat, while k < len |
| 74 | + while( k < len ) { |
| 75 | + |
| 76 | + var kValue; |
| 77 | + |
| 78 | + // a. Let Pk be ToString(k). |
| 79 | + // This is implicit for LHS operands of the in operator |
| 80 | + // b. Let kPresent be the result of calling the HasProperty internal method of O with argument Pk. |
| 81 | + // This step can be combined with c |
| 82 | + // c. If kPresent is true, then |
| 83 | + if ( Object.prototype.hasOwnProperty.call(O, k) ) { |
| 84 | + |
| 85 | + // i. Let kValue be the result of calling the Get internal method of O with argument Pk. |
| 86 | + kValue = O[ k ]; |
| 87 | + |
| 88 | + // ii. Call the Call internal method of callback with T as the this value and |
| 89 | + // argument list containing kValue, k, and O. |
| 90 | + callback.call( T, kValue, k, O ); |
| 91 | + } |
| 92 | + // d. Increase k by 1. |
| 93 | + k++; |
| 94 | + } |
| 95 | + // 8. return undefined |
| 96 | +}; |
0 commit comments