Skip to content

Commit

Permalink
Make sure that Array.of works when subclassed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 24, 2015
1 parent bfed50c commit e7c4505
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@
},

of: function of() {
return Array.from(arguments);
return Array.from.call(this, arguments);
}
};
defineProperties(Array, ArrayShims);
Expand Down Expand Up @@ -972,7 +972,8 @@
if (Array.prototype.values && Array.prototype.values.name !== 'values') {
var originalArrayPrototypeValues = Array.prototype.values;
defineProperty(Array.prototype, 'values', function values() { return originalArrayPrototypeValues.call(this); }, true);
Array.prototype[$iterator$] = Array.prototype.values;
defineProperty(Array.prototype, $iterator$, Array.prototype.values, true);
Value.preserveToString(Array.prototype.values, originalArrayPrototypeValues);
}
defineProperties(Array.prototype, ArrayPrototypeShims);

Expand Down
14 changes: 14 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@ var runArrayTests = function () {
it('should create correct array from arguments', function () {
expect(Array.of(1, null, undefined)).to.eql([1, null, undefined]);
});

it('should work with other constructors', function () {
var Foo = function (length) {
this.args = Array.prototype.slice.call(arguments, 1);
this.length = length;
};
var args = ['a', 'b', 'c'];
var expected = new Foo(args.length);
args.forEach(function (arg, index) {
expected[index] = arg;
});
expect(Array.of.apply(Foo, args)).to.eql(expected);
});

});

describe('Array#copyWithin', function () {
Expand Down

0 comments on commit e7c4505

Please sign in to comment.