Skip to main content

Bug Tracker

Side navigation

#11185 closed bug (patchwelcome)

Opened January 18, 2012 03:35AM UTC

Closed July 24, 2012 10:01PM UTC

isPlainObject gives wrong result in IE6/7/8 when constructor adds properties

Reported by: pcl Owned by:
Priority: low Milestone: 1.next
Component: core Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:
Description
#!js
Test = function() { this.a = 1; }
    
alert($.isPlainObject(new Test()); // false in all browsers

Test.prototype = { b: 1 };

alert($.isPlainObject(new Test()); // true in IE6/7/8

"Own properties are enumerated firstly" is apparently not the case in these versions of IE, so the property added in the constructor gets tested instead of a prototype property.

A workaround is to do this instead:

#!js
Test.prototype = $.extend(Test.prototype, {b: 1});

Which ensures this earlier check passes:

#!js
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf")

http://jsfiddle.net/CHwpg/2/

Attachments (0)
Change History (6)

Changed January 18, 2012 03:57AM UTC by dmethvin comment:1

resolution: → invalid
status: newclosed

If it's created via a constructor function, it's not really a plain object. The documentation says this:

http://api.jquery.com/jQuery.isPlainObject/

Check to see if an object is a plain object (created using "{}" or "new Object").

Changed January 18, 2012 04:03AM UTC by pcl comment:2

Replying to [comment:1 dmethvin]:

If it's created via a constructor function, it's not really a plain object.

Yes, the bug is that isPlainObject is returning true for what is not a plain object.

Changed January 18, 2012 04:04AM UTC by dmethvin comment:3

resolution: invalid
status: closedreopened

Oh hah! I read it backwards. Disregard, I suck ....

Changed March 10, 2012 02:09PM UTC by dmethvin comment:4

component: unfiledcore
milestone: None1.8
status: reopenedopen

Not sure this needs fixing, our use cases for isPlainObject don't require this level of differentiation. If it's easily fixable though, sure.

Changed May 18, 2012 01:28AM UTC by dmethvin comment:5

milestone: 1.81.next
priority: undecidedlow

Changed July 24, 2012 10:01PM UTC by dmethvin comment:6

resolution: → patchwelcome
status: openclosed

Since this method is intended primarily for our internal use, it doesn't seem like it's worth fixing because it works for our needs. As an example, $.extend uses it, but we already know that constructed objects are impossible to handle there.

If there are some specific critical situations where this fails, or you have a simple fix, let us know.