1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
Array.prototype.myCopyWithin = function(target,start,end) { var len = this.length target = target < 0 ? Math.abs(target) > len ? len : len + target : target > len ? len : target start = typeof start === 'number' ? start < 0 ? Math.abs(start) > len ? len : len + start : start > len ? len : start : 0 end = typeof end === 'number' ? end < 0 ? Math.abs(end) > len ? len : len + end : end > len ? len : end : len var oTarget = target var offset = end - start var arr = Array.prototype.mySlice.call(this) while (target < len && (target-oTarget) < offset && start < end) { if(!this[start])break this[target] = arr[start] start++ target++ } return this } console.log([1, 2, 3, 4, 5].myCopyWithin(-2)); console.log([1, 2, 3, 4, 5].copyWithin(-2)); console.log([1, 2, 3, 4, 5].myCopyWithin(0, 3)); console.log([1, 2, 3, 4, 5].copyWithin(0, 3)); console.log([1, 2, 3, 4, 5].myCopyWithin(0, 3, 4)); console.log([1, 2, 3, 4, 5].copyWithin(0, 3, 4)); console.log([1, 2, 3, 4, 5].myCopyWithin(-2, -3, -1)); console.log([1, 2, 3, 4, 5].copyWithin(-2, -3, -1)); console.log([1, 2, 3, 4, 5].myCopyWithin(3, 2, 4)); console.log([1, 2, 3, 4, 5].copyWithin(3, 2, 4)); console.log([].myCopyWithin.call({length: 5, 3: 1}, 0, 3)); console.log([].copyWithin.call({length: 5, 3: 1}, 0, 3)); console.log([].myCopyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4)); console.log([].copyWithin.call(new Int32Array([1, 2, 3, 4, 5]), 0, 3, 4));
|