-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add is-subsequence.md and operators.md files
- Loading branch information
1 parent
e0d742e
commit 419969f
Showing
2 changed files
with
45 additions
and
59 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
sidebar_position: 1 | ||
tags: | ||
- operators | ||
--- | ||
|
||
# Spread Operator… / Rest Parameter… | ||
|
||
***the rest parameter** is a feature that allows a function to accept an indefinite number of arguments* | ||
|
||
```jsx | ||
// Rest parameter | ||
var argumentsLength = function(...args) { | ||
// 也可以把 array like args 轉 array 使用 [...args] | ||
return args.length | ||
}; | ||
|
||
argumentsLength(1,2,3) // 3 | ||
``` | ||
|
||
***The spread operator** is used to unpack elements from an array or an iterable object (like a string or a set) into individual elements.* | ||
|
||
```jsx | ||
|
||
// Spread Operator | ||
let arr = [3]; | ||
let arr2 = [8, 9, 15]; | ||
|
||
let merged = [0, ...arr, 2, ...arr2]; | ||
|
||
alert(merged); // 0,3,2,8,9,15 | ||
|
||
// copy array | ||
let arr = [1, 2, 3]; | ||
let arrCopy = [...arr]; | ||
|
||
``` | ||
|
||
# 推薦閱讀 | ||
|
||
英文: Rest parameters and spread syntax (<https://javascript.info/rest-parameters-spread>) | ||
|
||
延伸考題 array-like object 跟 array 有什麼不同 ? | ||
|
||
像 function 裡的 arguments 是 array like object ,他可以使用 length、for…of、arguments[0] 等但卻不能使用 array 原生方法例如 push、forEach …等。若想要使用 array 原生方法必須使用 […arguments] 或Array.from() 轉成真正 array 才能使用 |