Permalink
Browse files

remove methods in Array.prototype so for ( x in [] ) works

  • Loading branch information...
1 parent 3287165 commit b078f98360c7ce7f4699c8938cc4fd77d0e71b97 @erh erh committed Aug 24, 2009
Showing with 16 additions and 11 deletions.
  1. +1 −1 jstests/indexa.js
  2. +15 −10 shell/utils.js
View
@@ -15,7 +15,7 @@ assert.eq( 2 , t.count() , "indexa 1" );
t.update( {x:'B'}, { x:'A' } );
a = t.find().toArray();
-u = a.map( function(z){ return z.x } ).unique();
+u = Array.unique( a.map( function(z){ return z.x } ) );
assert.eq( 2 , t.count() , "indexa 2" );
assert( a.length == u.length , "unique index update is broken" );
View
@@ -152,33 +152,35 @@ Date.prototype.tojson = function(){
RegExp.prototype.tojson = RegExp.prototype.toString;
-Array.prototype.contains = function( x ){
- for ( var i=0; i<this.length; i++ ){
- if ( this[i] == x )
+Array.contains = function( a , x ){
+ for ( var i=0; i<a.length; i++ ){
+ if ( a[i] == x )
return true;
}
return false;
}
-Array.prototype.unique = function( ){
+Array.unique = function( a ){
var u = [];
- for ( var i=0; i<this.length; i++){
- var o = this[i];
- if ( ! u.contains( o ) )
+ for ( var i=0; i<a.length; i++){
+ var o = a[i];
+ if ( ! Array.contains( u , o ) ){
u.push( o );
+ }
}
return u;
}
-Array.prototype.tojson = function( sepLines ){
+
+Array.tojson = function( a , sepLines ){
var s = "[";
if ( sepLines ) s += "\n";
- for ( var i=0; i<this.length; i++){
+ for ( var i=0; i<a.length; i++){
if ( i > 0 ){
s += ",";
if ( sepLines ) s += "\n";
}
- s += tojson( this[i] );
+ s += tojson( a[i] );
}
s += "]";
if ( sepLines ) s += "\n";
@@ -270,6 +272,9 @@ tojsonObject = function( x ){
if ( typeof( x.tojson ) == "function" && x.tojson != tojson )
return x.tojson();
+
+ if ( typeof( x.constructor.tojson ) == "function" && x.constructor.tojson != tojson )
+ return x.constructor.tojson( x );
if ( x.toString() == "[object MaxKey]" )
return "{ $maxKey : 1 }";

0 comments on commit b078f98

Please sign in to comment.