Переписал пример реализации выше без лишних проверок в надежде понять нафиг это нужно
if (!Array.prototype.copyWithin) {
Array.prototype.copyWithin = function(target, start, end) {
var length = this.length, count, direction,
to = target < 0 ? Math.max(length + target, 0) : Math.min(target, length),
from = start < 0 ? Math.max(length + start, 0) : Math.min(start, length);
end = typeof end == "undefined" ? length : end;
end = end < 0 ? Math.max(length + end, 0) : Math.min(end, length);
count = Math.min(end - from, length - to);
direction = 1;
if (from < to && to < (from + count)) {
direction = -1;
from += count - 1;
to += count - 1;
}
while (count > 0) {
if (from in this) {
this[to] = this[from];
}
else {
delete this[from];
}
from += direction;
to += direction;
count -= 1;
}
return this;
};
}
alert([
[1, 2, 3, 4, 5].copyWithin(0, 3).toString() == [4, 5, 3, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(1, 3).toString() == [1, 4, 5, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(1, 2).toString() == [1, 3, 4, 5, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(2, 2).toString() == [1, 2, 3, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(0, 3, 4).toString() == [4, 2, 3, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(1, 3, 4).toString() == [1, 4, 3, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(1, 2, 4).toString() == [1, 3, 4, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(0, -2).toString() == [4, 5, 3, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(0, -2, -1).toString() == [4, 2, 3, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(-4, -3, -2).toString() == [1, 3, 3, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(-4, -3, -1).toString() == [1, 3, 4, 4, 5].toString(),
[1, 2, 3, 4, 5].copyWithin(-4, -3).toString() == [1, 3, 4, 5, 5].toString(),
].every(function (testResult) {
return testResult;
}));