Changeset 12607
- Timestamp:
- 02/21/08 21:09:41 (11 months ago)
- Files:
-
- 1 modified
-
dojo/trunk/_base/array.js (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
dojo/trunk/_base/array.js
r11363 r12607 5 5 var _getParts = function(arr, obj, cb){ 6 6 return [ 7 (dojo.isString(arr) ? arr.split("") : arr),8 (obj||dojo.global),7 dojo.isString(arr) ? arr.split("") : arr, 8 obj || dojo.global, 9 9 // FIXME: cache the anonymous functions we create here? 10 (dojo.isString(cb) ? (new Function("item", "index", "array", cb)) : cb)10 dojo.isString(cb) ? new Function("item", "index", "array", cb) : cb 11 11 ]; 12 } 12 }; 13 13 14 14 dojo.mixin(dojo, { … … 45 45 }, 46 46 47 forEach: function(/*Array */arr, /*Function*/callback, /*Object?*/obj){47 forEach: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){ 48 48 // summary: 49 // for every item in arr, call callback with that item as its 50 // only parameter. 49 // for every item in arr, callback is invoked. Return values are ignored. 50 // arr: the array to iterate on. If a string, operates on individual characters. 51 // callback: a function is invoked with three arguments: item, index, and array 52 // thisObject: may be used to scope the call to callback 51 53 // description: 52 // Return values are ignored. This function53 // corresponds (and wraps) the JavaScript 1.6 forEach method. For54 // more details, see:54 // This function corresponds to the JavaScript 1.5 Array.forEach() method. 55 // In environments that support JavaScript 1.5, this function is a passthrough to the built-in method. 56 // For more details, see: 55 57 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach 56 58 … … 60 62 // FIXME: there are several ways of handilng thisObject. Is 61 63 // dojo.global always the default context? 62 var _p = _getParts(arr, obj, callback); arr = _p[0];64 var _p = _getParts(arr, thisObject, callback); arr = _p[0]; 63 65 for(var i=0,l=_p[0].length; i<l; i++){ 64 66 _p[2].call(_p[1], arr[i], i, arr); … … 66 68 }, 67 69 68 _everyOrSome: function(/*Boolean*/every, /*Array */arr, /*Function*/callback, /*Object?*/obj){69 var _p = _getParts(arr, obj, callback); arr = _p[0];70 _everyOrSome: function(/*Boolean*/every, /*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){ 71 var _p = _getParts(arr, thisObject, callback); arr = _p[0]; 70 72 for(var i = 0, l = arr.length; i < l; i++){ 71 73 var result = !!_p[2].call(_p[1], arr[i], i, arr); … … 77 79 }, 78 80 79 every: function(/*Array */arr, /*Function*/callback, /*Object?*/thisObject){81 every: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){ 80 82 // summary: 81 // Determines whether or not every item in the arraysatisfies the83 // Determines whether or not every item in arr satisfies the 82 84 // condition implemented by callback. 85 // arr: the array to iterate on. If a string, operates on individual characters. 86 // callback: a function is invoked with three arguments: item, index, and array and returns true 87 // if the condition is met. 88 // thisObject: may be used to scope the call to callback 83 89 // description: 84 // The parameter thisObject may be used to 85 // scope the call to callback. The function signature is derived 86 // from the JavaScript 1.6 Array.every() function. More 87 // information on this can be found here: 90 // This function corresponds to the JavaScript 1.5 Array.every() method. 91 // In environments that support JavaScript 1.5, this function is a passthrough to the built-in method. 92 // For more details, see: 88 93 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every 89 94 // example: … … 96 101 }, 97 102 98 some: function(/*Array */arr, /*Function*/callback, /*Object?*/thisObject){103 some: function(/*Array|String*/arr, /*Function|String*/callback, /*Object?*/thisObject){ 99 104 // summary: 100 // Determines whether or not any item in the arraysatisfies the105 // Determines whether or not any item in arr satisfies the 101 106 // condition implemented by callback. 107 // arr: the array to iterate on. If a string, operates on individual characters. 108 // callback: a function is invoked with three arguments: item, index, and array and returns true 109 // if the condition is met. 110 // thisObject: may be used to scope the call to callback 102 111 // description: 103 // The parameter thisObject may be used to 104 // scope the call to callback. The function signature is derived 105 // from the JavaScript 1.6 Array.some() function. More 106 // information on this can be found here: 112 // This function corresponds to the JavaScript 1.5 Array.some() method. 113 // In environments that support JavaScript 1.5, this function is a passthrough to the built-in method. 114 // For more details, see: 107 115 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:some 108 116 // example: … … 115 123 }, 116 124 117 map: function(/*Array */arr, /*Function*/func, /*Function?*/obj){125 map: function(/*Array|String*/arr, /*Function|String*/callback, /*Function?*/thisObject){ 118 126 // summary: 119 // applies a function to each element of an Array and creates127 // applies callback to each element of arr and returns 120 128 // an Array with the results 129 // arr: the array to iterate on. If a string, operates on individual characters. 130 // callback: a function is invoked with three arguments: item, index, and array and returns a value 131 // thisObject: may be used to scope the call to callback 121 132 // description: 122 // Returns a new array constituted from the return values of 123 // passing each element of arr into unary_func. The obj parameter 124 // may be passed to enable the passed function to be called in 125 // that scope. In environments that support JavaScript 1.6, this 126 // function is a passthrough to the built-in map() function 127 // provided by Array instances. For details on this, see: 128 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map 133 // This function corresponds to the JavaScript 1.5 Array.map() method. 134 // In environments that support JavaScript 1.5, this function is a passthrough to the built-in method. 135 // For more details, see: 136 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map 129 137 // example: 130 138 // | dojo.map([1, 2, 3, 4], function(item){ return item+1 }); 131 139 // returns [2, 3, 4, 5] 132 var _p = _getParts(arr, obj, func); arr = _p[0];133 var outArr = ( (arguments[3])? (new arguments[3]()) : []);140 var _p = _getParts(arr, thisObject, callback); arr = _p[0]; 141 var outArr = (arguments[3] ? (new arguments[3]()) : []); 134 142 for(var i=0;i<arr.length;++i){ 135 143 outArr.push(_p[2].call(_p[1], arr[i], i, arr)); … … 138 146 }, 139 147 140 filter: function(/*Array*/arr, /*Function */callback, /*Object?*/obj){148 filter: function(/*Array*/arr, /*Function|String*/callback, /*Object?*/thisObject){ 141 149 // summary: 142 150 // Returns a new Array with those items from arr that match the 143 // condition implemented by callback. ob may be used to 144 // scope the call to callback. The function signature is derived 145 // from the JavaScript 1.6 Array.filter() function. 146 // 147 // More information on the JS 1.6 API can be found here: 151 // condition implemented by callback. 152 // arr: the array to iterate on. If a string, operates on individual characters. 153 // callback: a function is invoked with three arguments: item, index, and array and returns true 154 // if the condition is met. 155 // thisObject: may be used to scope the call to callback 156 // description: 157 // This function corresponds to the JavaScript 1.5 Array.filter() method. 158 // In environments that support JavaScript 1.5, this function is a passthrough to the built-in method. 159 // For more details, see: 148 160 // http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:filter 149 161 // example: … … 151 163 // returns [2, 3, 4] 152 164 153 var _p = _getParts(arr, obj, callback); arr = _p[0];165 var _p = _getParts(arr, thisObject, callback); arr = _p[0]; 154 166 var outArr = []; 155 167 for(var i = 0; i < arr.length; i++){