Skip to main content

Bug Tracker

Side navigation

#317 closed bug (fixed)

Opened October 27, 2006 03:35PM UTC

Closed December 03, 2006 11:07AM UTC

Last modified March 14, 2012 08:56PM UTC

Unreachable code in jQuery constructor function.

Reported by: henra..h+jQuery@gmai Owned by:
Priority: major Milestone: 1.1a
Component: core Version: 1.1a
Keywords: Cc:
Blocked by: Blocking:
Description

If a new jQuery object is created with the usual syntax ($('div', document.body)), the constructor function doesn't get far before it realises that it needs to call the ''new'' keyword on itself, with this line:

	if ( window == this )
		return new jQuery(a,c);

However, as you can see, only the two formal parameters are passed into this second call -- that is, any extra arguments are discarded. This would not be a problem of course, except that later in the function there is a provision for an extra function that may be passed in:

	// See if an extra function was provided
	var fn = arguments[ arguments.length - 1 ];

	// If so, execute it in context
	if ( fn && typeof fn == "function" )
		this.each(fn);

By the time this code is reached, the extra un-named argument will have been discarded, whether or not it was present in the original call.

This bug could be circumvented if the original statement explicitly instantiated a new object (new jQuery('div', document.body, someFunction)) -- but since $(etc) is the conventional syntax, this feature is effectively unreachable by a normal jQuery call.

Attachments (0)
Change History (2)

Changed November 17, 2006 09:25PM UTC by john comment:1

description: If a new jQuery object is created with the usual syntax (`$('div', document.body)`), the constructor function doesn't get far before it realises that it needs to call the ''new'' keyword on itself, with this line:\ {{{\ if ( window == this )\ return new jQuery(a,c);\ }}}\ However, as you can see, only the two formal parameters are passed into this second call -- that is, any extra arguments are discarded. This would not be a problem of course, except that later in the function there is a provision for an extra function that may be passed in:\ {{{\ // See if an extra function was provided\ var fn = arguments[ arguments.length - 1 ];\ \ // If so, execute it in context\ if ( fn && typeof fn == "function" )\ this.each(fn);\ }}}\ By the time this code is reached, the extra un-named argument will have been discarded, whether or not it was present in the original call.\ \ This bug could be circumvented if the original statement explicitly instantiated a new object (`new jQuery('div', document.body, someFunction)`) -- but since $(etc) is the conventional syntax, this feature is effectively unreachable by a normal jQuery call.If a new jQuery object is created with the usual syntax (`$('div', document.body)`), the constructor function doesn't get far before it realises that it needs to call the ''new'' keyword on itself, with this line:\ {{{\ if ( window == this )\ return new jQuery(a,c);\ }}}\ However, as you can see, only the two formal parameters are passed into this second call -- that is, any extra arguments are discarded. This would not be a problem of course, except that later in the function there is a provision for an extra function that may be passed in:\ {{{\ // See if an extra function was provided\ var fn = arguments[ arguments.length - 1 ];\ \ // If so, execute it in context\ if ( fn && typeof fn == "function" )\ this.each(fn);\ }}}\ By the time this code is reached, the extra un-named argument will have been discarded, whether or not it was present in the original call.\ \ This bug could be circumvented if the original statement explicitly instantiated a new object (`new jQuery('div', document.body, someFunction)`) -- but since $(etc) is the conventional syntax, this feature is effectively unreachable by a normal jQuery call.
milestone: → 1.1
priority: minormajor
version: → 1.1

Changed December 03, 2006 11:07AM UTC by joern comment:2

resolution: → fixed
status: newclosed

Fixed in SVN, the unreachable code is now removed.