From 6ca84f1794af830ada9736d7289ce29aabb04ca3 Mon Sep 17 00:00:00 2001 From: t7yang Date: Mon, 10 Jan 2022 08:38:05 +0800 Subject: remove `notranslate` class in zh-TW --- .../reference/operators/spread_syntax/index.html | 36 +++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) (limited to 'files/zh-tw/web/javascript/reference/operators/spread_syntax') diff --git a/files/zh-tw/web/javascript/reference/operators/spread_syntax/index.html b/files/zh-tw/web/javascript/reference/operators/spread_syntax/index.html index fe6ea9a383..42793870c0 100644 --- a/files/zh-tw/web/javascript/reference/operators/spread_syntax/index.html +++ b/files/zh-tw/web/javascript/reference/operators/spread_syntax/index.html @@ -15,16 +15,16 @@ translation_of: Web/JavaScript/Reference/Operators/Spread_syntax

用在呼叫函式時:

-
myFunction(...iterableObj);
+
myFunction(...iterableObj);
 

用在陣列或字串時:

-
[...iterableObj, '4', 'five', 6];
+
[...iterableObj, '4', 'five', 6];

用在物件時(new in ECMAScript 2018):

-
let objClone = { ...obj };
+
let objClone = { ...obj };

Rest syntax (parameters)

@@ -38,19 +38,19 @@ translation_of: Web/JavaScript/Reference/Operators/Spread_syntax

It is common to use {{jsxref("Function.prototype.apply()")}} in cases where you want to use the elements of an array as arguments to a function.

-
function myFunction(x, y, z) { }
+
function myFunction(x, y, z) { }
 const args = [0, 1, 2];
 myFunction.apply(null, args);

With spread syntax the above can be written as:

-
function myFunction(x, y, z) { }
+
function myFunction(x, y, z) { }
 const args = [0, 1, 2];
 myFunction(...args);

Any argument in the argument list can use spread syntax, and the spread syntax can be used multiple times.

-
function myFunction(v, w, x, y, z) { }
+
function myFunction(v, w, x, y, z) { }
 const args = [0, 1];
 myFunction(-1, ...args, 2, ...[3]);
@@ -58,13 +58,13 @@ myFunction(-1, ...args, 2, ...[3]);

When calling a constructor with {{jsxref("Operators/new", "new")}} it's not possible to directly use an array and apply() (apply() does a [[Call]] and not a [[Construct]]). However, an array can be easily used with new thanks to spread syntax:

-
const dateFields = [1970, 0, 1];  // 1 Jan 1970
+
const dateFields = [1970, 0, 1];  // 1 Jan 1970
 const d = new Date(...dateFields);
 

To use new with an array of parameters without spread syntax, you would have to do it indirectly through partial application:

-
function applyAndNew(constructor, args) {
+
function applyAndNew(constructor, args) {
    function partial () {
       return constructor.apply(this, args);
    };
@@ -96,7 +96,7 @@ console.log(new myConstructorWithArguments);
 
 

Without spread syntax, to create a new array using an existing array as one part of it, the array literal syntax is no longer sufficient and imperative code must be used instead using a combination of {{jsxref("Array.prototype.push", "push()")}}, {{jsxref("Array.prototype.splice", "splice()")}}, {{jsxref("Array.prototype.concat", "concat()")}}, etc. With spread syntax this becomes much more succinct:

-
const parts = ['shoulders', 'knees'];
+
const parts = ['shoulders', 'knees'];
 const lyrics = ['head', ...parts, 'and', 'toes'];
 //  ["head", "shoulders", "knees", "and", "toes"]
 
@@ -105,7 +105,7 @@ const lyrics = ['head', ...parts, 'and', 'toes'];

Copy an array

-
const arr = [1, 2, 3];
+
const arr = [1, 2, 3];
 const arr2 = [...arr]; // like arr.slice()
 
 arr2.push(4);
@@ -116,7 +116,7 @@ arr2.push(4);
 

Note: Spread syntax effectively goes one level deep while copying an array. Therefore, it may be unsuitable for copying multidimensional arrays, as the following example shows. (The same is true with {{jsxref("Object.assign()")}} and spread syntax.)

-
const a = [[1], [2], [3]];
+
const a = [[1], [2], [3]];
 const b = [...a];
 
 b.shift().shift();
@@ -132,7 +132,7 @@ a
 
 

{{jsxref("Array.prototype.concat()")}} is often used to concatenate an array to the end of an existing array. Without spread syntax, this is done as:

-
const arr1 = [0, 1, 2];
+
const arr1 = [0, 1, 2];
 const arr2 = [3, 4, 5];
 
 //  Append all items from arr2 onto arr1
@@ -140,7 +140,7 @@ arr1 = arr1.concat(arr2);

With spread syntax this becomes:

-
let arr1 = [0, 1, 2];
+
let arr1 = [0, 1, 2];
 let arr2 = [3, 4, 5];
 
 arr1 = [...arr1, ...arr2];
@@ -150,7 +150,7 @@ arr1 = [...arr1, ...arr2];
 
 

{{jsxref("Array.prototype.unshift()")}} is often used to insert an array of values at the start of an existing array. Without spread syntax, this is done as:

-
const arr1 = [0, 1, 2];
+
const arr1 = [0, 1, 2];
 const arr2 = [3, 4, 5];
 
 //  Prepend all items from arr2 onto arr1
@@ -160,7 +160,7 @@ Array.prototype.unshift.apply(arr1, arr2)
 
 

With spread syntax, this becomes:

-
let arr1 = [0, 1, 2];
+
let arr1 = [0, 1, 2];
 let arr2 = [3, 4, 5];
 
 arr1 = [...arr2, ...arr1];
@@ -177,7 +177,7 @@ arr1 = [...arr2, ...arr1];
 
 

Shallow-cloning (excluding prototype) or merging of objects is now possible using a shorter syntax than {{jsxref("Object.assign()")}}.

-
const obj1 = { foo: 'bar', x: 42 };
+
const obj1 = { foo: 'bar', x: 42 };
 const obj2 = { foo: 'baz', y: 13 };
 
 const clonedObj = { ...obj1 };
@@ -190,7 +190,7 @@ const mergedObj = { ...obj1, ...obj2 };
 
 

Note that you cannot replace or mimic the {{jsxref("Object.assign()")}} function:

-
let obj1 = { foo: 'bar', x: 42 };
+
let obj1 = { foo: 'bar', x: 42 };
 let obj2 = { foo: 'baz', y: 13 };
 const merge = ( ...objects ) => ( { ...objects } );
 
@@ -208,7 +208,7 @@ let mergedObj2 = merge ({}, obj1, obj2);
 
 

Spread syntax (other than in the case of spread properties) can be applied only to iterable objects:

-
const obj = {'key1': 'value1'};
+
const obj = {'key1': 'value1'};
 const array = [...obj]; // TypeError: obj is not iterable
 
-- cgit v1.2.3-54-g00ecf