Opened 15 years ago
Closed 14 years ago
#3026 closed enhancement (duplicate)
More robust jQuery.makeArray
Reported by: | arrix | Owned by: | flesler |
---|---|---|---|
Priority: | minor | Milestone: | 1.3 |
Component: | core | Version: | 1.2.6 |
Keywords: | Cc: | arrix | |
Blocked by: | Blocking: |
Description
Currently, makeArray() uses array.call to determine whether array is a function, which fails with scripts that define Array.prototype.call.
makeArray: function( array ) { var ret = []; if( array != null ){ var i = array.length; //the window, strings and functions also have 'length' if( i == null || array.split || array.setInterval || array.call ) ret[0] = array; else while( i ) ret[--i] = array[i]; } return ret; },
Unfortunately, there are quite a few scripts do so, notably, effect.js (an old version)
Search for Array.prototype.call
Is it OK to use jQuery.isFunction(array) instead of a simple property test? If jQuery.isFunction() introduces too much overhead, we can also use something like typeof array == 'function'.
Instead of testing whether array is not an Array, we can also test whether it is an Array. e.g. if (array && array.length && array.splice && array.join)
This may seem unnecessary if the developer has full control of all javascript code but it makes jQuery more robust (good for bookmarklets and greasemonkey scripts).
Change History (4)
comment:1 Changed 15 years ago by
Owner: | set to flesler |
---|---|
Priority: | major → minor |
Status: | new → assigned |
Type: | bug → enhancement |
comment:2 Changed 15 years ago by
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
The only known conflict is with Scriptaculous. But they changed they removed call from Array's prototype since 1.8.
So upgrading solves the problem, no need for a hack on our side.
comment:3 Changed 14 years ago by
Resolution: | wontfix |
---|---|
Status: | closed → reopened |
comment:4 Changed 14 years ago by
Cc: | arrix added |
---|---|
Resolution: | → duplicate |
Status: | reopened → closed |
This has been taken into account in the end. Fixed at [5825].
There are many different types of collections that must be treated as arrays.
Arrays, arguments, nodelists, jQuery, form.elements, {length:1, '0':'a'} and probably more.
Testing positively for arrays would make it impossible to handle all this. Using isFunction does seem like too much overhead for me.
This was already "scheduled for improvement". Will work on it asap, for the next release.