Skip to content

Instantly share code, notes, and snippets.

@Naddiseo
Created August 13, 2013 04:22
Show Gist options
  • Save Naddiseo/6217887 to your computer and use it in GitHub Desktop.
Save Naddiseo/6217887 to your computer and use it in GitHub Desktop.
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