8000 Make object iteration that uses `_.keys` work correctly in IE < 9 and… · lodash/lodash@14343b5 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 14343b5

Browse files
committed
Make object iteration that uses _.keys work correctly in IE < 9 and _.isPlainObject work correctly in IE < 8.
Former-commit-id: ad9a3c36acb38e36cd21fe82a29b7e65a767e049
1 parent ffdac30 commit 14343b5

File tree

1 file changed

+40
-39
lines changed
10000

1 file changed

+40
-39
lines changed

lodash.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@
364364
* @type Boolean
365365
*/
366366
try {
367-
support.nodeClass = !(toString.call(document) == objectClass && !String({ 'toString': 0 }));
367+
support.nodeClass = !(toString.call(document) == objectClass && !({ 'toString': 0 } + ''));
368368
} catch(e) {
369369
support.nodeClass = true;
370370
}
@@ -510,22 +510,22 @@
510510
' <%= loop %>;' +
511511
' <% if (support.enumPrototypes || useHas) { %>\n }<% } %>\n' +
512512
' }' +
513-
' <% } %>' +
514513

515514
// Because IE < 9 can't set the `[[Enumerable]]` attribute of an
516515
// existing property and the `constructor` property of a prototype
517516
// defaults to non-enumerable, Lo-Dash skips the `constructor`
518517
// property when it infers it's iterating over a `prototype` object.
519-
' <% if (support.nonEnumShadows) { %>\n\n' +
518+
' <% if (support.nonEnumShadows) { %>\n\n' +
520519
' var ctor = iterable.constructor;\n' +
521-
' <% for (var k = 0; k < 7; k++) { %>\n' +
520+
' <% for (var k = 0; k < 7; k++) { %>\n' +
522521
" index = '<%= shadowedProps[k] %>';\n" +
523522
' if (<%' +
524523
" if (shadowedProps[k] == 'constructor') {" +
525524
' %>!(ctor && ctor.prototype === iterable) && <%' +
526525
' } %>hasOwnProperty.call(iterable, index)) {\n' +
527526
' <%= loop %>\n' +
528527
' }' +
528+
' <% } %>' +
529529
' <% } %>' +
530530
' <% } %>' +
531531
' <% if (arrays || support.nonEnumArgs) { %>\n}<% } %>\n' +
@@ -753,22 +753,6 @@
753753
);
754754
}
755755

756-
/**
757-
* A function compiled to iterate `arguments` objects, arrays, objects, and
758-
* strings consistenly across environments, executing the `callback` for each
759-
* element in the `collection`. The `callback` is bound to `thisArg` and invoked
760-
* with three arguments; (value, index|key, collection). Callbacks may exit
761-
* iteration early by explicitly returning `false`.
762-
*
763-
* @private
764-
* @type Function
765-
* @param {Array|Object|String} collection The collection to iterate over.
766-
* @param {Function} [callback=identity] The function called per iteration.
767-
* @param {Mixed} [thisArg] The `this` binding of `callback`.
768-
* @returns {Array|Object|String} Returns `collection`.
769-
*/
770-
var each = createIterator(eachIteratorOptions);
771-
772756
/**
773757
* Used by `template` to escape characters for inclusion in compiled
774758
* string literals.
@@ -802,7 +786,7 @@
802786
function isNode(value) {
803787
// IE < 9 presents DOM nodes as `Object` objects except they have `toString`
804788
// methods that are `typeof` "string" and still can coerce nodes to strings
805-
return typeof value.toString != 'function' && typeof String(value) == 'string';
789+
return typeof value.toString != 'function' && typeof (value + '') == 'string';
806790
}
807791

808792
/**
@@ -845,7 +829,8 @@
845829
}
846830
// check that the constructor is `Object` (i.e. `Object instanceof Object`)
847831
var ctor = value.constructor;
848-
if ((!isFunction(ctor) && (support.nodeClass || !isNode(value))) || ctor instanceof ctor) {
832+
833+
if (isFunction(ctor) ? ctor instanceof ctor : (support.nodeClass || !isNode(value))) {
849834
// IE < 9 iterates inherited properties before own properties. If the first
850835
// iterated property is an object's own property then there are no inherited
851836
// enumerable properties.
@@ -867,23 +852,6 @@
867852 return result;
868853
}
869854

870-
/**
871-
* A fallback implementation of `Object.keys` that produces an array of the
872-
* given object's own enumerable property names.
873-
*
874-
* @private
875-
* @type Function
876-
* @param {Object} object The object to inspect.
877-
* @returns {Array} Returns a new array of property names.
878-
*/
879-
var shimKeys = createIterator({
880-
'args': 'object',
881-
'init': '[]',
882-
'top': 'if (!(objectTypes[typeof object])) return result',
883-
'loop': 'result.push(index)',
884-
'arrays': false
885-
});
886-
887855
/**
888856
* Slices the `collection` from the `start` index up to, but not including,
889857
* the `end` index.
@@ -973,6 +941,23 @@
973941
return (support.argsObject && value instanceof Array) || toString.call(value) == arrayClass;
974942
};
975943

944+
/**
945+
* A fallback implementation of `Object.keys` that produces an array of the
946+
* given object's own enumerable property names.
947+
*
948+
* @private
949+
* @type Function
950+
* @param {Object} object The object to inspect.
951+
* @returns {Array} Returns a new array of property names.
952+
*/
953+
var shimKeys = createIterator({
954+
'args': 'object',
955+
'init': '[]',
956+
'top': 'if (!(objectTypes[typeof object])) return result',
957+
'loop': 'result.push(index)',
958+
'arrays': false
959+
});
960+
976961
/**
977962
* Creates an array composed of the own enumerable property names of `object`.
978963
*
@@ -997,6 +982,22 @@
997982
return nativeKeys(object);
998983
};
999984

985+
/**
986+
* A function compiled to iterate `arguments` objects, arrays, objects, and
987+
* strings consistenly across environments, executing the `callback` for each
988+
* element in the `collection`. The `callback` is bound to `thisArg` and invoked
989+
* with three arguments; (value, index|key, collection). Callbacks may exit
990+
* iteration early by explicitly returning `false`.
991+
*
992+
* @private
993+
* @type Function
994+
* @param {Array|Object|String} collection The collection to iterate over.
995+
* @param {Function} [callback=identity] The function called per iteration.
996+
* @param {Mixed} [thisArg] The `this` binding of `callback`.
997+
* @returns {Array|Object|String} Returns `collection`.
998+
*/
999+
var each = createIterator(eachIteratorOptions);
1000+
10001001
/**
10011002
* Used to convert characters to HTML entities:
10021003
*

0 commit comments

Comments
 (0)
0