Side navigation
#10458 closed bug (wontfix)
Opened October 09, 2011 06:49AM UTC
Closed October 09, 2011 02:31PM UTC
Last modified April 06, 2013 09:50AM UTC
jQuery.isPlainObject() always returns false when Object.prototype has a properties
Reported by: | vovan-ve@yandex.ru | Owned by: | |
---|---|---|---|
Priority: | low | Milestone: | None |
Component: | core | Version: | 1.6.4 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
jQuery.isPlainObject() finally enumerates all propertyes of an object. If the last property is own, then function relies that it is an Object. As the result, it is not allowed to have any properties in prototype. So, if we extend Object.prototype with at least one property, jQuery.isPlainObject() will always return false for new objects, created after.
jsFiddle: http://jsfiddle.net/kqe95/
alert(jQuery.isPlainObject({})); // true Object.prototype.foo = function(){}; alert(jQuery.isPlainObject({})); // false
Attachments (0)
Change History (10)
Changed October 09, 2011 02:31PM UTC by comment:1
component: | unfiled → core |
---|---|
priority: | undecided → low |
resolution: | → wontfix |
status: | new → closed |
Changed April 03, 2013 07:18PM UTC by comment:2
This is still not fixed!
Besides the function does a lot of "stuff" and its performance is very low!
It is extensively used by $.extend when creating deep copies!
Why not simply comparing either object.__proto__ == Object.prototype or in IE Object.getPrototypeOf(object) == Object.prototype
if Object.getPrototypeOf is missing as well, the current implementation could be good enough
Tests are here: http://jsfiddle.net/LLKET/1/
Changed April 03, 2013 11:02PM UTC by comment:3
Right... it's not fixed because I closed it as "won't fix", because jQuery won't fix bugs that are caused by user code that does stupid things like extending Object.prototype
Changed April 04, 2013 11:32AM UTC by comment:4
Replying to [comment:3 rwaldron]:
... user code that does stupid things like extending Object.prototype
I still think, that You are not right. Can you give a prooflink to confirm this words?
Changed April 04, 2013 01:15PM UTC by comment:5
Why my above approach is not a good fix for all modern browsers?
Changed April 04, 2013 01:44PM UTC by comment:6
Replying to [comment:5 laurentiu.macovei@…]:
Why my above approach is not a good fix for all modern browsers?
function Foo() {}; Foo.prototype = Object.prototype; alert( new Foo().__proto__ === Object.prototype );
2. one_window.Object !== another_window.Object;
/* this is window A */ window.FooBar = {}; open('<url to run next code in B>'); /* this is window B opened by window.open() in A */ alert( opener.Object === Object ); // false alert( opener.FooBar.__proto__ === Object.prototype ); // false alert( opener.FooBar.__proto__ === opener.Object.prototype ); // true
Changed April 05, 2013 10:21AM UTC by comment:7
That's correct but I think you really don't care about that. It may be useful in 0.001% of cases, whereas you could use the current option.
Changed April 05, 2013 03:03PM UTC by comment:8
Replying to [comment:4 vovan-ve]:
Replying to [comment:3 rwaldron]: > ... user code that does stupid things like extending Object.prototype I still think, that You are not right. Can you give a prooflink to confirm this words?
jQuery's Won't Fix: http://contribute.jquery.org/wont-fix/
The problem with adding properties to Object.prototype is that all Object's will then inherit those properties, whether they want to or not—and these properties will appear in for-in loops.
Since ES5, Object.prototype can be safely augmented with Object.definePropert(y|ies) by creating a properties and specifying that its [[enumerable]] field is false.
- "Object.prototype is verboten" http://erik.eae.net/archives/2005/06/06/22.13.54/
- "Deep-extending objects in JavaScript" http://andrewdupont.net/2009/08/28/deep-extending-objects-in-javascript/
- "Extending JavaScript Natives" http://javascriptweblog.wordpress.com/2011/12/05/extending-javascript-natives/
- "Extending built-in native objects. Evil or not?" http://perfectionkills.com/extending-built-in-native-objects-evil-or-not/
Changed April 05, 2013 04:30PM UTC by comment:9
Replying to [comment:8 rwaldron]:
Replying to [comment:4 vovan-ve]: - "Object.prototype is verboten" http://erik.eae.net/archives/2005/06/06/22.13.54/ ...
Thanks for the links. Now I see I'm not right.
Changed April 06, 2013 09:50AM UTC by comment:10
Even so, jQuery will simply fail when Object.prototype will have something added.
But that can be sorted out with ES5 Object.defineProperty("someProperty", { enumerable: false});
http://docs.jquery.com/Won't_Fix