Bug Tracker

Modify

Ticket #3026 (closed enhancement: duplicate)

Opened 5 years ago

Last modified 5 years ago

More robust jQuery.makeArray

Reported by: arrix Owned by: flesler
Priority: minor Milestone: 1.3
Component: core Version: 1.2.6
Keywords: Cc: arrix
Blocking: Blocked by:

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

comment:1 Changed 5 years ago by flesler

  • Owner set to flesler
  • Priority changed from major to minor
  • Status changed from new to assigned
  • Type changed from bug to enhancement

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.

comment:2 Changed 5 years ago by flesler

  • Status changed from assigned to closed
  • Resolution set to wontfix

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 5 years ago by flesler

  • Status changed from closed to reopened
  • Resolution wontfix deleted

comment:4 Changed 5 years ago by flesler

  • Cc arrix added
  • Status changed from reopened to closed
  • Resolution set to duplicate

This has been taken into account in the end. Fixed at [5825].

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.