[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/slice [Back]  [Original]

Array.prototype.slice() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.prototype.slice()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

slice() , .

In this article

const animals = ["ant", "bison", "camel", "duck", "elephant"];

console.log(animals.slice(2));
// Expected output: Array ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));
// Expected output: Array ["camel", "duck"]

console.log(animals.slice(1, 5));
// Expected output: Array ["bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2));
// Expected output: Array ["duck", "elephant"]

console.log(animals.slice(2, -1));
// Expected output: Array ["camel", "duck"]

console.log(animals.slice());
// Expected output: Array ["ant", "bison", "camel", "duck", "elephant"]

arr.slice([begin[, end]])

begin

( ), .

, begin . slice(-2) .

begin , slice() 0.

begin .

end

( ), . slice() end.

slice(1, 4) ( 1, 2 3).

, end . slice(2, -1) .

end >= array.length end , slice() (arr.length).

, .

slice() , , , . :

, .

:

js
// :      
var fruits = ["", "", "", "", ""];
var citrus = fruits.slice(1, 3);

// citrus  ['', '']

: slice()

slice() , newCar, myCar. myHonda. myHonda , .

js
//  slice,  newCar  myCar.
var myHonda = {
  color: "",
  wheels: 4,
  engine: { cylinders: 4, size: 2.2 },
};
var myCar = [myHonda, 2, "  ", "  1997"];
var newCar = myCar.slice(0, 2);

//   myCar, newCar   myHonda
//      .
console.log("myCar = " + myCar.toSource());
console.log("newCar = " + newCar.toSource());
console.log("myCar[0].color = " + myCar[0].color);
console.log("newCar[0].color = " + newCar[0].color);

//   myHonda.
myHonda.color = "";
console.log("   Honda - " + myHonda.color);

//   myHonda     .
console.log("myCar[0].color = " + myCar[0].color);
console.log("newCar[0].color = " + newCar[0].color);

:

js
myCar = [{color:'', wheels:4, engine:{cylinders:4, size:2.2}}, 2,
         '  ', '  1997']
newCar = [{color:'', wheels:4, engine:{cylinders:4, size:2.2}}, 2]
myCar[0].color = 
newCar[0].color = 
   Honda - 
myCar[0].color = 
newCar[0].color = 

slice() / Array. . arguments .

js
function list() {
  return Array.prototype.slice.call(arguments, 0);
}

var list1 = list(1, 2, 3); // [1, 2, 3]

call() Function, [].slice.call(arguments) Array.prototype.slice.call(). , bind().

js
var unboundSlice = Array.prototype.slice;
var slice = Function.prototype.call.bind(unboundSlice);

function list() {
  return slice(arguments, 0);
}

var list1 = list(1, 2, 3); // [1, 2, 3]

-

- (, DOM) Mozilla Array.prototype.slice() IE < 9 , IE, 9- . - . , IE, Mozilla, Chrome, Safari Opera, , ( DOM) slice(), , ; , , , - . ( IE, slice(), null/undefined, IE , , IE >= 9, .)

js
/**
 *   ""    IE < 9  slice
 *  -  NamedNodeMap, NodeList  HTMLCollection
 * (,  -   ,
 *   ,  ES2015, IE    ).
 *    ,   IE < 9,    undefined
 *   (  Firefox),   ,  
 *     DOM.
 */
(function () {
  "use strict";
  var _slice = Array.prototype.slice;

  try {
    //      DOM  IE < 9
    _slice.call(document.documentElement);
  } catch (e) {
    //  IE < 9  
    //      ,  ,
    // NamedNodeMap (, , ),
    // NodeList (, getElementsByTagName), HTMLCollection (, childNodes)
    //        DOM (     DOM  IE < 9)
    Array.prototype.slice = function (begin, end) {
      // IE < 9    end,  undefined
      end = typeof end !== "undefined" ? end : this.length;

      //    Array     slice
      if (Object.prototype.toString.call(this) === "[object Array]") {
        return _slice.call(this, begin, end);
      }

      //     
      var i,
        cloned = [],
        size,
        len = this.length;

      //    begin
      var start = begin || 0;
      start = start >= 0 ? start : len + start;

      //    end
      var upTo = end ? end : len;
      if (end < 0) {
        upTo = len + end;
      }

      //    
      size = upTo - start;

      if (size > 0) {
        cloned = new Array(size);
        if (this.charAt) {
          for (i = 0; i < size; i++) {
            cloned[i] = this.charAt(start + i);
          }
        } else {
          for (i = 0; i < size; i++) {
            cloned[i] = this[start + i];
          }
        }
      }

      return cloned;
    };
  }
})();

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.slice


Web Proxy Viewer  |  New URL  |  Original Page