Skip to content

Commit 3bbf03f

Browse files
committed
feat(isArray): support Array subclasses in angular.isArray
Closes angular#15533 BREAKING CHANGE: `angular.isArray` is used by `angular.copy`, which in turn is used internally by the dirty checking logic. That's why this change affects the way objects are copied and watched by AngularJS. Some objects that weren't previously recognized as arrays (e.g. MobX observable arrays, see angular#15533) by the watching logic are now recognized as such. This change also affects `angular.merge`, `angular.forEach`, and `angular.equals`. Previously, `angular.isArray` was an alias for and thus worked exactly as `Array.isArray`.
1 parent 8b69d91 commit 3bbf03f

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

Diff for: src/Angular.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ function isArrayLike(obj) {
219219

220220
// NodeList objects (with `item` method) and
221221
// other objects with suitable length characteristics are array-like
222-
return isNumber(length) &&
223-
(length >= 0 && ((length - 1) in obj || obj instanceof Array) || typeof obj.item === 'function');
222+
return isNumber(length) && (length >= 0 && (length - 1) in obj || typeof obj.item === 'function');
224223

225224
}
226225

@@ -635,12 +634,14 @@ function isDate(value) {
635634
* @kind function
636635
*
637636
* @description
638-
* Determines if a reference is an `Array`. Alias of Array.isArray.
637+
* Determines if a reference is an `Array`.
639638
*
640639
* @param {*} value Reference to check.
641640
* @returns {boolean} True if `value` is an `Array`.
642641
*/
643-
var isArray = Array.isArray;
642+
function isArray(arr) {
643+
return arr instanceof Array || Array.isArray(arr);
644+
}
644645

645646
/**
646647
* @description

Diff for: test/AngularSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,16 @@ describe('angular', function() {
12511251
});
12521252
});
12531253

1254+
describe('isArray', function() {
1255+
1256+
it('should return true if passed an object prototypically inherited from Array.prototype', function() {
1257+
function FooArray() {}
1258+
FooArray.prototype = [];
1259+
expect(isArray(new FooArray())).toBe(true);
1260+
});
1261+
1262+
});
1263+
12541264
describe('isArrayLike', function() {
12551265

12561266
it('should return false if passed a number', function() {

0 commit comments

Comments
 (0)