Side navigation
#6132 closed enhancement (wontfix)
Opened February 20, 2010 04:22PM UTC
Closed November 19, 2010 11:10AM UTC
jQuery.each includes prototype functions
| Reported by: | agoebel | Owned by: | |
|---|---|---|---|
| Priority: | undecided | Milestone: | 1.4.2 |
| Component: | core | Version: | 1.4.1 |
| Keywords: | each, object, prototype | Cc: | agoebel@swordandspade.com |
| Blocked by: | Blocking: |
Description
I'm not entirely sure if this is deliberate or not, but the way jQuery handles the each method includes anything defined in the prototype.
Example:
#!js
Object.prototype.newMethod = function() {};
...
var keys = [],
map = {
'a': 1,
'b': 2,
'c': 3
};
var
jQuery.each( map, function(k,v) {
keys.push(k);
};
the keys array has a value of:
['a','b','c','newMethod']
The change is pretty trivial. Simply add a hasOwnProperty filter to the object portion of the each method.
#!js
//starting from line 556
if ( isObj ) {
for ( name in object ) {
if ( !object.hasOwnProperty(name) ) {
continue;
}
if ( callback.apply( object[ name ], args ) === false ) {
break;
}
}
}
Using this code, keys is:
['a','b','c']
Which I personally think is what it should be. As an alternative, maybe a flag can be introduced to determine whether or not prototype values are filtered out.
The modified jquery-1.4.2 file is attached. I tested the example and patched code in firefox 3.6, chrome 4.0, and IE8.
Attachments (1)
Change History (1)
Changed November 19, 2010 11:10AM UTC by comment:1
| priority: | → undecided |
|---|---|
| resolution: | → wontfix |
| status: | new → closed |
A user can use
hasOwnPropertyto filter out anything from the prototype that they want to already; this patch only limits the ability for users to decide whether or not they want to access prototype members too.