Skip to content

Commit

Permalink
Multiple lines for array.slice syntax (#2351)
Browse files Browse the repository at this point in the history
* array.slice syntax

* Remove comments from syntax

As discussed in #2202 (comment)
  • Loading branch information
hamishwillee authored Feb 16, 2021
1 parent 9c24b4a commit 7b673ea
Showing 1 changed file with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
title: Array.prototype.slice()
slug: Web/JavaScript/Reference/Global_Objects/Array/slice
tags:
- Array
- JavaScript
- Method
- Prototype
- Reference
- Array
- JavaScript
- Method
- Prototype
- Reference
---
<div>{{JSRef}}</div>

Expand All @@ -24,7 +24,9 @@

<h2 id="Syntax">Syntax</h2>

<pre class="brush: js notranslate"><var>arr</var>.slice([<var>start</var>[, <var>end</var>]])
<pre class="brush: js">slice()
slice(start)
slice(start, end)
</pre>

<h3 id="Parameters">Parameters</h3>
Expand Down Expand Up @@ -79,7 +81,7 @@ <h2 id="Examples">Examples</h2>

<h3 id="Return_a_portion_of_an_existing_array">Return a portion of an existing array</h3>

<pre class="brush: js notranslate">let fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
<pre class="brush: js">let fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
let citrus = fruits.slice(1, 3)

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
Expand All @@ -93,7 +95,7 @@ <h3 id="Using_slice">Using slice</h3>
When the color of <code>myHonda</code> is changed to purple, both arrays reflect the
change.</p>

<pre class="brush: js notranslate">// Using slice, create newCar from myCar.
<pre class="brush: js">// Using slice, create newCar from myCar.
let myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }
let myCar = [myHonda, 2, 'cherry condition', 'purchased 1997']
let newCar = myCar.slice(0, 2)
Expand All @@ -116,7 +118,7 @@ <h3 id="Using_slice">Using slice</h3>

<p>This script writes:</p>

<pre class="brush: js notranslate">myCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2,
<pre class="brush: js">myCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2,
'cherry condition', 'purchased 1997']
newCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2]
myCar[0].color = red
Expand All @@ -133,21 +135,21 @@ <h3 id="Array-like_objects">Array-like objects</h3>
object. The {{jsxref("Functions/arguments", "arguments")}} inside a function is an
example of an 'array-like object'.</p>

<pre class="brush: js notranslate">function list() {
<pre class="brush: js">function list() {
return Array.prototype.slice.call(arguments)
}

let list1 = list(1, 2, 3) // [1, 2, 3]
</pre>

<p>Binding can be done with the {{jsxref("Function.prototype.call", "call()")}} method of
{{jsxref("Function.prototype")}} and it can also be reduced using
{{jsxref("Function")}} and it can also be reduced using
<code>[].slice.call(arguments)</code> instead of
<code>Array.prototype.slice.call</code>.</p>

<p>Anyway, it can be simplified using {{jsxref("Function.prototype.bind", "bind")}}.</p>

<pre class="brush: js notranslate">let unboundSlice = Array.prototype.slice
<pre class="brush: js">let unboundSlice = Array.prototype.slice
let slice = Function.prototype.call.bind(unboundSlice)

function list() {
Expand Down

0 comments on commit 7b673ea

Please sign in to comment.