// works with array-like objects var args = (function () { return Array.prototype.slice.call(arguments); }(1, 2, 3)); var argsClass = Object.prototype.toString.call(args);
assertDeepEq(args, [1, 2, 3]);
Array.prototype.copyWithin.call(args, -2, 0);
assertDeepEq(args, [1, 1, 2]);
assertDeepEq(Object.prototype.toString.call(args), argsClass);
// throws on null/undefined values
assertThrowsInstanceOf(function() {
Array.prototype.copyWithin.call(null, 0, 3);
}, TypeError, "Assert that copyWithin fails if this value is null");
assertThrowsInstanceOf(function() {
Array.prototype.copyWithin.call(undefined, 0, 3);
}, TypeError, "Assert that copyWithin fails if this value is undefined");
// test with this value as string
assertThrowsInstanceOf(function() {
Array.prototype.copyWithin.call("hello world", 0, 3);
}, TypeError, "Assert that copyWithin fails if this value is string");
// test with this value as number
assertDeepEq(Array.prototype.copyWithin.call(34, 0, 3), new Number(34));
// test with this value as TypedArray var buffer = new ArrayBuffer(16); var int32View = new Int32Array(buffer); for (var i=0; i<int32View.length; i++) {
int32View[i] = i*2;
}
assertDeepEq(Array.prototype.copyWithin.call(int32View, 0, 1), new Int32Array([2, 4, 6, 6]));
// if arguments object is sloppy, copyWithin must move the arguments around function f(a, b, c, d, e) {
[].copyWithin.call(arguments, 1, 3); return [a, b, c, d, e];
}
assertDeepEq(f(1, 2, 3, 4, 5), [1, 4, 5, 4, 5]);
// test with target > start on 2 arguments
assertDeepEq([1, 2, 3, 4, 5].copyWithin(3, 0), [1, 2, 3, 1, 2]);
// test with target > start on 3 arguments
assertDeepEq([1, 2, 3, 4, 5].copyWithin(3, 0, 4), [1, 2, 3, 1, 2]);
// test on array with holes var arr = new Array(6); for (var i = 0; i < arr.length; i += 2) {
arr[i] = i;
}
assertDeepEq(arr.copyWithin(0, 3), [, 4, , , 4, , ]);
// test on fractional arguments
assertDeepEq([1, 2, 3, 4, 5].copyWithin(0.2, 3.9), [4, 5, 3, 4, 5]);
// test with -0
assertDeepEq([1, 2, 3, 4, 5].copyWithin(-0, 3), [4, 5, 3, 4, 5]);
// test with arguments more than this.length
assertDeepEq([1, 2, 3, 4, 5].copyWithin(0, 7), [1, 2, 3, 4, 5]);
// test with arguments less than -this.length
assertDeepEq([1, 2, 3, 4, 5].copyWithin(-7, 0), [1, 2, 3, 4, 5]);
// test with arguments equal to -this.length
assertDeepEq([1, 2, 3, 4, 5].copyWithin(-5, 0), [1, 2, 3, 4, 5]);
// test on empty array
assertDeepEq([].copyWithin(0, 3), []);
// test with target range being shorter than end - start
assertDeepEq([1, 2, 3, 4, 5].copyWithin(2, 1, 4), [1, 2, 2, 3, 4]);
// undefined as third argument
assertDeepEq([1, 2, 3, 4, 5].copyWithin(0, 3, undefined), [4, 5, 3, 4, 5]);
// test that this.length is called only once
arr = {0: 1, 1: 2, 2: 3, 3: 4, 4: 5}; var count = 0;
Object.defineProperty(arr, "length", {
get: function () {
count++;
}
});
Array.prototype.copyWithin.call(arr, 1, 3);
assertEq(count, 1);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.