Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Commit

Permalink
fix(forEach): should ignore prototypically inherited properties
Browse files Browse the repository at this point in the history
Closes #813
  • Loading branch information
IgorMinar committed Mar 22, 2012
1 parent 5fdab52 commit 8d7e694
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@ function forEach(obj, iterator, context) {
for (key = 0; key < obj.length; key++)
iterator.call(context, obj[key], key);
} else {
for (key in obj)
iterator.call(context, obj[key], key);
for (key in obj) {
if (obj.hasOwnProperty(key)) {
iterator.call(context, obj[key], key);
}
}
}
}
return obj;
Expand Down
19 changes: 19 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,25 @@ describe('angular', function() {
});
});


describe('forEach', function() {
it('should iterate over *own* object properties', function() {
function MyObj() {
this.bar = 'barVal';
this.baz = 'bazVal';
};
MyObj.prototype.foo = 'fooVal';

var obj = new MyObj(),
log = [];

forEach(obj, function(value, key) { log.push(key + ':' + value)});

expect(log).toEqual(['bar:barVal', 'baz:bazVal']);
});
});


describe('sortedKeys', function() {
it('should collect keys from object', function() {
expect(sortedKeys({c:0, b:0, a:0})).toEqual(['a', 'b', 'c']);
Expand Down

0 comments on commit 8d7e694

Please sign in to comment.