Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7894)

Unified Diff: chrome/renderer/resources/extensions/utils.js

Issue 12440030: Use utils.forEach everywhere rather than Array.prototype.forEach to guard (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: make foreach of an array give numbers Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/renderer/resources/extensions/utils.js
diff --git a/chrome/renderer/resources/extensions/utils.js b/chrome/renderer/resources/extensions/utils.js
index ed35e15f9fcf11e06d2a2a5792ff35c4a580d03a..50134eadb569f2b40a5ebb3c6f033eae1b1efa28 100644
--- a/chrome/renderer/resources/extensions/utils.js
+++ b/chrome/renderer/resources/extensions/utils.js
@@ -4,10 +4,20 @@
var chrome = requireNative('chrome').GetChrome();
-function forEach(dict, f) {
- for (var key in dict) {
- if (dict.hasOwnProperty(key))
- f(key, dict[key]);
+function forEach(obj, f, self) {
+ // For arrays, make sure the indices are numbers not strings - and in that
+ // case this method can be more efficient by assuming the array isn't sparse.
+ //
+ // Note: not (instanceof Array) because the array might come from a different
+ // context than our own.
+ if (obj.constructor && obj.constructor.name == 'Array') {
+ for (var i = 0; i < obj.length; i++)
+ f.call(self, i, obj[i]);
+ } else {
+ for (var key in obj) {
+ if (obj.hasOwnProperty(key))
+ f.call(self, key, obj[key]);
+ }
}
}
« no previous file with comments | « chrome/renderer/resources/extensions/tag_watcher.js ('k') | chrome/renderer/resources/extensions/web_view.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698