Skip to main content

Bug Tracker

Side navigation

#9091 closed bug (wontfix)

Opened May 04, 2011 09:54AM UTC

Closed July 12, 2011 05:07PM UTC

Last modified March 14, 2012 05:29AM UTC

isPlainObject and Firefox 4: can't convert undefined to object

Reported by: Riccardo "Rial" Re Owned by: rwaldron
Priority: low Milestone: 1.next
Component: core Version: 1.6
Keywords: Cc:
Blocked by: Blocking:
Description

I have discovered a problem in the isPlainObject of jQuery.

If I use as an argument of that function a simple variable like:{id:1,name:'wow'} i have an object with the .constructor initialized but without .constructor.prototype

This cause an exeception in Firefox 4.0.1 (Chrome and Safari are fine).

To handle it the library can be modified with a simple check.

The result is:

isPlainObject: function( obj ) {
		// Must be an Object.
		// Because of IE, we also have to check the presence of the constructor property.
		// Make sure that DOM nodes and window objects don't pass through, as well
		if ( !obj || jQuery.type(obj) !== "object" || obj.nodeType || jQuery.isWindow( obj ) ) {
			return false;
		}

		// Not own constructor property must be Object
		if ( obj.constructor &&
			!hasOwn.call(obj, "constructor") &&
//Changed code: added a check to dodge the exception 
			obj.constructor.prototype &&
//End of change
			!hasOwn.call(obj.constructor.prototype, "isPrototypeOf") ) {
			return false;
		}

		// Own properties are enumerated firstly, so to speed up,
		// if last one is own, then all properties are own.

		var key;
		for ( key in obj ) {}

		return key === undefined || hasOwn.call( obj, key );
	},
Attachments (0)
Change History (7)

Changed May 04, 2011 09:56AM UTC by addyosmani comment:1

component: unfiledmisc
owner: → Riccardo "Rial" Re
priority: undecidedlow
status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced test case on http://jsFiddle.net that reproduces the issue experienced to help us assess your ticket!

Additionally, test against the jQuery (edge) version to ensure the issue still exists.

Changed May 06, 2011 05:12AM UTC by anonymous comment:2

I got this error too.. FF4.0.1 with jQuery 1.6

Changed May 11, 2011 10:44AM UTC by Riccardo "Rial" Re comment:3

status: pendingnew

Test case prepared:

http://jsfiddle.net/yU3f9/3/

Changed May 17, 2011 03:04AM UTC by rwaldron comment:4

component: misccore
owner: Riccardo "Rial" Rerwaldron
status: newassigned

I'll take this to at least test the solution provided by the OP.. seems like it might be vendor based

Changed July 12, 2011 05:07PM UTC by rwaldron comment:5

resolution: → wontfix
status: assignedclosed

This is extremely edge case - there are hoisting issues, setting object.prototype.constructor, creating objects with brute-forced constructors...

Further reduced: http://jsfiddle.net/rwaldron/6Mtrw/

Changed July 12, 2011 05:07PM UTC by rwaldron comment:6

"If you build a boat with a hole in, it _will_ sink"

Changed September 21, 2011 09:19PM UTC by reesd comment:7

FYI, For others finding this issue while trying to understand this misleading error message. This can also happen in Firefox if you pass a chrome/protected object to a content/unprotected instance of jQuery's isObject. isObject gets called from extend which gets called from all over (including whenever you pass a options object)...

For example, if you do the following in a Firefox extension:

content.wrappedJSObject.jQuery.isObject({})

"If you leave a hole in the ground, someone will fall into it :)"