diff options
| author | Igor Minar | 2012-03-21 15:42:35 -0700 |
|---|---|---|
| committer | Igor Minar | 2012-03-22 16:39:36 -0700 |
| commit | 8d7e6948496ff26ef1da8854ba02fcb8eebfed61 (patch) | |
| tree | 23cbc4d26de03a2ddca5867a3d75f4650d8e16d3 | |
| parent | 5fdab52dd7c269f99839f4fa6b5854d9548269fa (diff) | |
| download | angular.js-8d7e6948496ff26ef1da8854ba02fcb8eebfed61.tar.bz2 | |
fix(forEach): should ignore prototypically inherited properties
Closes #813
| -rw-r--r-- | src/Angular.js | 7 | ||||
| -rw-r--r-- | test/AngularSpec.js | 19 |
2 files changed, 24 insertions, 2 deletions
diff --git a/src/Angular.js b/src/Angular.js index 4ba95d10..371a56c0 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -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; diff --git a/test/AngularSpec.js b/test/AngularSpec.js index 92949b66..cb0c5c6b 100644 --- a/test/AngularSpec.js +++ b/test/AngularSpec.js @@ -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']); |
