Opened 11 years ago
Closed 11 years ago
#11185 closed bug (patchwelcome)
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
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:
Test.prototype = $.extend(Test.prototype, {b: 1});
Which ensures this earlier check passes:
!hasOwn.call(obj.constructor.prototype, "isPrototypeOf")
Change History (6)
comment:1 follow-up: 2 Changed 11 years ago by
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 Changed 11 years ago by
Replying to 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.
comment:3 Changed 11 years ago by
Resolution: | invalid |
---|---|
Status: | closed → reopened |
Oh hah! I read it backwards. Disregard, I suck ....
comment:4 Changed 11 years ago by
Component: | unfiled → core |
---|---|
Milestone: | None → 1.8 |
Status: | reopened → open |
Not sure this needs fixing, our use cases for isPlainObject
don't require this level of differentiation. If it's easily fixable though, sure.
comment:5 Changed 11 years ago by
Milestone: | 1.8 → 1.next |
---|---|
Priority: | undecided → low |
comment:6 Changed 11 years ago by
Resolution: | → patchwelcome |
---|---|
Status: | open → closed |
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.
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/