plugd icon indicating copy to clipboard operation
plugd copied to clipboard

Added hasOwnProperty to forIn

Open OverZealous opened this issue 14 years ago • 0 comments

I've wrapped the callback method in a hasOwnProperty check. This is generally considered good behavior, as it prevents modification of the default prototype from affecting hashmaps. Link to the JSLint page on the subject.

Example (really) bad code:

// modify the base prototype
Object.prototype.foo = "bar";

// create a map to iterator over
var hashmap = { baz: "foo" };

// iterates over all inherited properties (for better or for worse)
for(var k in hashmap) {
    console.log(hashmap[k]);
}
// outputs:
//     bar
//     foo

Now, I suppose it could be argued that the check should be optional.

OverZealous avatar Sep 22 '11 07:09 OverZealous