Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step

Home Page:https://juejin.cn/column/7244788137410560055

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array.prototype.unshift

Sunny-117 opened this issue · comments

commented
Array.prototype.myUnshift = function (...items) {
    this.reverse().push(...items.reverse())
    this.reverse()
    return this.length
}
Array.prototype.unshift = function() {
  var itemsToAdd = arguments.length;
  var len = this.length;
  for (var i = len - 1; i >= 0; i--) {
    this[i + itemsToAdd] = this[i];
  }
  for (var j = 0; j < itemsToAdd; j++) {
    this[j] = arguments[j];
  }
  return this.length;
};