private Object iterativeMethod(Context cx,int id,Scriptable scope,Scriptable thisObj,Object[] args){
  Object callbackArg=args.length > 0 ? args[0] : Undefined.instance;
  if (callbackArg == null || !(callbackArg instanceof Function)) {
    throw ScriptRuntime.notFunctionError(callbackArg);
  }
  Function f=(Function)callbackArg;
  Scriptable parent=ScriptableObject.getTopLevelScope(f);
  Scriptable thisArg;
  if (args.length < 2 || args[1] == null || args[1] == Undefined.instance) {
    thisArg=parent;
  }
 else {
    thisArg=ScriptRuntime.toObject(cx,scope,args[1]);
  }
  long length=getLengthProperty(cx,thisObj);
  int resultLength=id == Id_map ? (int)length : 0;
  Scriptable array=cx.newArray(scope,resultLength);
  long j=0;
  for (long i=0; i < length; i++) {
    Object[] innerArgs=new Object[3];
    Object elem=getRawElem(thisObj,i);
    if (elem == Scriptable.NOT_FOUND) {
      continue;
    }
    innerArgs[0]=elem;
    innerArgs[1]=Long.valueOf(i);
    innerArgs[2]=thisObj;
    Object result=f.call(cx,parent,thisArg,innerArgs);
switch (id) {
case Id_every:
      if (!ScriptRuntime.toBoolean(result))       return Boolean.FALSE;
    break;
case Id_filter:
  if (ScriptRuntime.toBoolean(result))   setElem(cx,array,j++,innerArgs[0]);
break;
case Id_forEach:
break;
case Id_map:
setElem(cx,array,i,result);
break;
case Id_some:
if (ScriptRuntime.toBoolean(result)) return Boolean.TRUE;
break;
}
}
switch (id) {
case Id_every:
return Boolean.TRUE;
case Id_filter:
case Id_map:
return array;
case Id_some:
return Boolean.FALSE;
case Id_forEach:
default :
return Undefined.instance;
}
}

private Object iterativeMethod ( Context cx , int id , Scriptable scope , Scriptable thisObj , Object [ ] args ) { Object callbackArg = args . length > 0 ? args [ 0 ] : Undefined . instance ; if ( callbackArg == null || ! ( callbackArg instanceof Function ) ) { throw ScriptRuntime . notFunctionError ( callbackArg ) ; } Function f = ( Function ) callbackArg ; Scriptable parent = ScriptableObject . getTopLevelScope ( f ) ; Scriptable thisArg ; if ( args . length < 2 || args [ 1 ] == null || args [ 1 ] == Undefined . instance ) { thisArg = parent ; } else { thisArg = ScriptRuntime . toObject ( cx , scope , args [ 1 ] ) ; } long length = getLengthProperty ( cx , thisObj ) ; int resultLength = id == Id_map ? ( int ) length : 0 ; Scriptable array = cx . newArray ( scope , resultLength ) ; long j = 0 ; for ( long i = 0 ; i < length ; i ++ ) { Object [ ] innerArgs = new Object [ 3 ] ; Object elem = getRawElem ( thisObj , i ) ; if ( elem == Scriptable . NOT_FOUND ) { continue ; } innerArgs [ 0 ] = elem ; innerArgs [ 1 ] = Long . valueOf ( i ) ; innerArgs [ 2 ] = thisObj ; Object result = f . call ( cx , parent , thisArg , innerArgs ) ; switch ( id ) { case Id_every : if ( ! ScriptRuntime . toBoolean ( result ) ) return Boolean . FALSE ; break ; case Id_filter : if ( ScriptRuntime . toBoolean ( result ) ) setElem ( cx , array , j ++ , innerArgs [ 0 ] ) ; break ; case Id_forEach : break ; case Id_map : setElem ( cx , array , i , result ) ; break ; case Id_some : if ( ScriptRuntime . toBoolean ( result ) ) return Boolean . TRUE ; break ; } } switch ( id ) { case Id_every : return Boolean . TRUE ; case Id_filter : case Id_map : return array ; case Id_some : return Boolean . FALSE ; case Id_forEach : default : return Undefined . instance ; } }
implements the methods " every " , " filter " , " foreach " , " map " , and " some " .
