-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-1.html
87 lines (80 loc) · 2.98 KB
/
10-1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="./public/favicon.ico" />
<meta http-equiv="cache-control" content="no-cache" />
<title></title>
<link rel="stylesheet" href=" https://necolas.github.io/normalize.css/8.0.1/normalize.css" />
<link rel="stylesheet" href="./hightlight/default.min.css" />
<link rel="stylesheet" href="./css/main.css" />
<link rel="stylesheet" href="./css/copybutton.css" />
<link rel="stylesheet" href="./css/hightlight.css" />
<script src="./hightlight/hightlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BEVZJDBC7Z"></script>
<script src="./js/gtag.js"></script>
</head>
<body>
<header>
<nav>
<h1>
<span id="toggle-menu"></span>
<a href="index.html"></a>
</h1>
</nav>
</header>
<main>
<aside>
<nav></nav>
</aside>
<article>
<h2>新標準 ES2016 (ES7)</h2>
<h2 id="10-1">10-1 Array.prototype.includes()</h2>
<p>ES7 之前我們判斷一個陣列裡面是否包含指定元素</p>
<pre><code class="language-js">
let arr = ['JOJO', 'Joseph Joestar', 'Caesar', 'Kars'];
if (arr.indexOf('JOJO') !== -1) {
console.log('Hi~ JOJO');
}
</code></pre>
<p>ES7 之後可以寫成這樣,會更加直覺‧</p>
<pre><code class="language-js">
let arr = ['JOJO', 'Joseph Joestar', 'Caesar', 'Kars'];
if (arr.includes('JOJO')) { // ES7 Array.prototype.includes()
console.log('Hi~ JOJO');
}
</code></pre>
<p>實際應用做條件判斷部分情境下可以代換 if..else</p>
<pre><code class="language-js">
let conditionsA =[(()=>{return true;})(),false,(()=>{return false;})()]
console.log(conditionsA.includes(true)) // true
let conditionsB = [(()=>{return false;})(),false,(()=>{return false;})()]
console.log(conditionsB.includes(true)) // false
</code></pre>
<pre><code class="language-js">
console.log([NaN].includes(NaN)); // true
console.log([null].includes(null)); // true
console.log([undefined].includes(undefined)); // true
// 物件因為不同參考故無法做比對
// 若要比對必須指向同一參考變數。
console.log([{a:1}].includes({a:1})); // false
const obj = {a:1}
console.log([obj].includes(obj)); // true
// 萬國碼比對
const array = ['🥗', '🍔', '🍰'];
array.includes('🍰'); // true
// Symbol 比對
const symbol = Symbol('symbol');
const array = [symbol];
array.includes(symbol); // true
[Symbol('symbol')].includes(Symbol('symbol')) // false
</code></pre>
</article>
</main>
</body>
<script type="module" src="./js/main.js"></script>
</html>