Skip to content
Browse files

Avoid augmenting Array.prototype when possible.

Use Object.defineProperty to define Array.prototype.reduce as a
non-enumerable property on platforms that support it.
  • Loading branch information...
1 parent 6090b93 commit f37f62a557ebd0fc5f676ab570a480961b9c5178 Chris Osborn committed Aug 7, 2011
Showing with 43 additions and 13 deletions.
  1. +14 −3 q.js
  2. +10 −7 test/all.js
  3. +16 −0 test/issue/22.js
  4. +3 −3 test/issue/9.js
View
17 q.js
@@ -91,14 +91,25 @@ var keys =
keys.push(key);
return keys;
};
-var reduce =
- Array.prototype.reduce =
- Array.prototype.reduce || function (callback, basis) {
+var defineProperty =
+ Object.defineProperty =
+ Object.defineProperty || function(obj, prop, descriptor) {
+ // sadly, this one can't be faked very well
+ obj[prop] = descriptor.value;
+ };
+var reduce = Array.prototype.reduce;
+if (!reduce) {
+ reduce = function (callback, basis) {
for (var i = 0, ii = this.length; i < ii; i++) {
basis = callback(basis, this[i], i);
}
return basis;
};
+ defineProperty(Array.prototype, 'reduce', {
+ value: reduce,
+ enumerable: false
+ });
+}
var isStopIteration = function (exception) {
return Object.prototype.toString.call(exception)
=== "[object StopIteration]";
View
17 test/all.js
@@ -2,12 +2,15 @@
exports['test valueOf, isPromise, isResolved'] = require('./value-of');
exports['test reject'] = require('./reject');
-exports['test rosolve & reject'] = require('./resolve-reject')
-exports['test chains of promises'] = require('./promised-chains')
-exports['test multiple listeners'] = require('./multiple-listeners')
-exports['test methods'] = require('./methods')
-exports['test thenable'] = require('./thenable')
+exports['test rosolve & reject'] = require('./resolve-reject');
+exports['test chains of promises'] = require('./promised-chains');
+exports['test multiple listeners'] = require('./multiple-listeners');
+exports['test methods'] = require('./methods');
+exports['test thenable'] = require('./thenable');
-if (module == require.main)
- require('test').run(exports)
+exports['test GH issue 9'] = require('./issue/9');
+exports['test GH issue 22'] = require('./issue/22');
+if (module == require.main) {
+ require('test').run(exports);
+}
View
16 test/issue/22.js
@@ -0,0 +1,16 @@
+
+var Q = require('../../q');
+
+exports['test'] = function (ASSERT) {
+
+ // make sure Array.prototype is intact
+ var keys = [];
+ for (item in []) {
+ keys.push(item);
+ }
+ ASSERT.deepEqual(keys, [], 'no unexpected items in Array.prototype');
+};
+
+if (module == require.main) {
+ require('test').run(exports);
+}
View
6 test/issue/9.js
@@ -8,6 +8,6 @@ exports['test'] = function (ASSERT) {
ASSERT.ok(Q.isResolved(), 'omitted argument is a fully resolved value');
};
-if (module == require.main)
- require('test').run(exports)
-
+if (module == require.main) {
+ require('test').run(exports);
+}

0 comments on commit f37f62a

Please sign in to comment.
Something went wrong with that request. Please try again.