Revision: 4402
Author:   jasvir
Date:     Tue Mar 22 15:39:16 2011
Log: Implement Array.forEach on browsers that do not provide it for ES53's internal use.
http://codereview.appspot.com/4274066

ES53 uses forEach internally distinct from the version it exposes to cajoled
code. This CL provides a version .forEach for browsers that do not provide it.

See http://code.google.com/p/google-caja/issues/detail?id=1328

[email protected]

http://code.google.com/p/google-caja/source/detail?r=4402

Modified:
 /trunk/src/com/google/caja/es53.js

=======================================
--- /trunk/src/com/google/caja/es53.js  Thu Mar  3 22:49:23 2011
+++ /trunk/src/com/google/caja/es53.js  Tue Mar 22 15:39:16 2011
@@ -181,6 +181,24 @@
         return slice.call(dis, startIndex);
       }
     });
+
+  // Missing on IE
+  if (!Array.prototype.forEach) {
+    Array.prototype.forEach = function(fun) { //, thisp
+      var dis = ToObject(this);
+      var len = dis.length >>> 0;
+      if ('function' !== typeof fun) {
+        throw new TypeError("Expected function but got " + (typeof fun));
+      }
+
+      var thisp = arguments[1];
+      for (var i = 0; i < len; i++) {
+        if (i in dis) {
+          fun.call(thisp, dis[i], i, dis);
+        }
+      }
+    };
+  }

   ////////////////////////////////////////////////////////////////////////
   // Functions to walk the prototype chain
@@ -3975,7 +3993,7 @@
     });

   // 15.4.4.18
-  createOrWrap(Array.prototype, 'forEach', function (block, thisp) {
+  virtualize(Array.prototype, 'forEach', function (block, thisp) {
       var len = this.length >>> 0;
       for (var i = 0; i < len; i++) {
         if (i in this) {

Reply via email to