mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
fix(forEach): should ignore prototypically inherited properties
Closes #813
This commit is contained in:
parent
5fdab52dd7
commit
8d7e694849
2 changed files with 24 additions and 2 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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']);
|
||||
|
|
|
|||
Loading…
Reference in a new issue