Created
August 13, 2013 04:22
-
-
Save Naddiseo/6217887 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var prototypeOfArray = Array.prototype; | |
var prototypeOfObject = Object.prototype; | |
var _Array_slice_ = prototypeOfArray.slice; | |
var array_splice = Array.prototype.splice; | |
ArraySplice = function(start, deleteCount) { | |
var result | |
, args = _Array_slice_.call(arguments, 2) | |
, addElementsCount = args.length | |
; | |
if (!arguments.length) { | |
return []; | |
} | |
if (start === void 0) { // default | |
start = 0; | |
} | |
if (deleteCount === void 0) { // default | |
deleteCount = this.length - start; | |
} | |
if (addElementsCount > 0) { | |
if (deleteCount <= 0) { | |
if (start == this.length) { // tiny optimisation #1 | |
this.push.apply(this, args); | |
return []; | |
} | |
if (start == 0) { // tiny optimisation #2 | |
this.unshift.apply(this, args); | |
return []; | |
} | |
} | |
// Array.prototype.splice implementation | |
result = _Array_slice_.call(this, start, start + deleteCount);// delete part | |
args.push.apply(args, _Array_slice_.call(this, start + deleteCount, this.length));// right part | |
args.unshift.apply(args, _Array_slice_.call(this, 0, start));// left part | |
// delete all items from this array and replace it to 'left part' + _Array_slice_.call(arguments, 2) + 'right part' | |
args.unshift(0, this.length); | |
array_splice.apply(this, args); | |
return result; | |
} | |
return array_splice.call(this, start, deleteCount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment