|
1 | 1 | <div align="center">
|
2 | 2 | <img height="60" src="https://img.icons8.com/color/344/javascript.png">
|
3 | 3 | <h1>JavaScript 進階題目列表</h1>
|
4 |
| - |
5 | 4 | ---
|
6 | 5 |
|
7 |
| -<span>我會在我的 [Instagram](https://www.instagram.com/theavocoder) 上發布關於 JavaScript 的複選題,同時也會更新到這個 Repo 當中。更新日期: <a href=#20191224><b>2019 年 12 月 24 日</b></a> |
| 6 | +<span>我會在我的 [Instagram](https://www.instagram.com/theavocoder) 上發布關於 JavaScript 的複選題,同時也會更新到這個 Repo 當中。更新日期: <a href=#20200612><b>2020 年 06 月 12 日</b></a> |
8 | 7 |
|
9 | 8 | 從基礎到進階程度,測試你有多了解 JavaScript,不僅更新你的知識,更能幫助你的 coding 面試!
|
10 | 9 | :muscle: :rocket: 我每週都會在這個 Repo 中更新新的題目。
|
|
16 | 15 |
|
17 | 16 | </div>
|
18 | 17 |
|
| 18 | +--- |
| 19 | + |
| 20 | +歡迎在項目中使用它們 😃 我 _真的_ 很感激這個repo的參考,我創造了問題和解釋(是的,我很傷心lol),社區幫助我如此之多地維護和改進它!我很喜歡這個repo。 💪🏼 謝謝你,祝你玩得開心! |
19 | 21 |
|
20 | 22 | ---
|
21 | 23 |
|
@@ -2047,3 +2049,333 @@ multiply(value);
|
2047 | 2049 | </details>
|
2048 | 2050 |
|
2049 | 2051 | ---
|
| 2052 | + |
| 2053 | +###### 66. 使用哪個建構式可以成功繼承 Dog 類別? |
| 2054 | + |
| 2055 | +```javascript |
| 2056 | +class Dog { |
| 2057 | + constructor(name) { |
| 2058 | + this.name = name; |
| 2059 | + } |
| 2060 | +}; |
| 2061 | + |
| 2062 | +class Labrador extends Dog { |
| 2063 | + // 1 |
| 2064 | + constructor(name, size) { |
| 2065 | + this.size = size; |
| 2066 | + } |
| 2067 | + // 2 |
| 2068 | + constructor(name, size) { |
| 2069 | + super(name); |
| 2070 | + this.size = size; |
| 2071 | + } |
| 2072 | + // 3 |
| 2073 | + constructor(size) { |
| 2074 | + super(name); |
| 2075 | + this.size = size; |
| 2076 | + } |
| 2077 | + // 4 |
| 2078 | + constructor(name, size) { |
| 2079 | + this.name = name; |
| 2080 | + this.size = size; |
| 2081 | + } |
| 2082 | + |
| 2083 | +}; |
| 2084 | +``` |
| 2085 | + |
| 2086 | +- A: 1 |
| 2087 | +- B: 2 |
| 2088 | +- C: 3 |
| 2089 | +- D: 4 |
| 2090 | + |
| 2091 | +<details><summary><b>答案</b></summary> |
| 2092 | +<p> |
| 2093 | + |
| 2094 | +#### 答案: B |
| 2095 | + |
| 2096 | +在子類別中,在呼叫 `super` 前不能存取 `this` 關鍵字,如果你這麼做,它將拋出一個 `ReferenceError`,建構式1與4會引發這個錯誤。 |
| 2097 | + |
| 2098 | +使用 `super` 關鍵字時,我們要提供參數給父類別呼叫其建構式。父類別需要接受一個 `name` 參數,所以我們需要把 `name` 傳給 `super`。 |
| 2099 | + |
| 2100 | +`Labrador` 類別接收兩個參數, `name` 參數是由於它繼承了 `Dog` , `size` 作為`Labrador` 類的額外屬性,它們都需要傳遞給 `Labrador` 的建構式,因此使用建構式2是正確答案。 |
| 2101 | + |
| 2102 | +</p> |
| 2103 | +</details> |
| 2104 | + |
| 2105 | +--- |
| 2106 | + |
| 2107 | +###### 67. 將會輸出什麽內容? |
| 2108 | + |
| 2109 | +```javascript |
| 2110 | +// index.js |
| 2111 | +console.log('running index.js'); |
| 2112 | +import { sum } from './sum.js'; |
| 2113 | +console.log(sum(1, 2)); |
| 2114 | + |
| 2115 | +// sum.js |
| 2116 | +console.log('running sum.js'); |
| 2117 | +export const sum = (a, b) => a + b; |
| 2118 | +``` |
| 2119 | + |
| 2120 | +- A: `running index.js`, `running sum.js`, `3` |
| 2121 | +- B: `running sum.js`, `running index.js`, `3` |
| 2122 | +- C: `running sum.js`, `3`, `running index.js` |
| 2123 | +- D: `running index.js`, `undefined`, `running sum.js` |
| 2124 | + |
| 2125 | +<details><summary><b>答案</b></summary> |
| 2126 | +<p> |
| 2127 | + |
| 2128 | +#### 答案: B |
| 2129 | + |
| 2130 | +`import` 命令是 _編譯階段_ 執行的。這代表被引入的模組會優先執行,而引入模組的檔案會 _之後執行_。 |
| 2131 | + |
| 2132 | +這是 `CommonJS` 中 `require()` 和 `import` 之間的區別!使用 `require()`,您可以在執行程式時根據需要戴入依賴的項目。如果我們使用 `require` 而不是 `import` 來執行此題, 結果將會依 `running index.js`,`running sum.js`,`3` 的順序輸出。 |
| 2133 | + |
| 2134 | +</p> |
| 2135 | +</details> |
| 2136 | + |
| 2137 | +--- |
| 2138 | +###### 68. 將會輸出什麽內容? |
| 2139 | + |
| 2140 | +```javascript |
| 2141 | +console.log(Number(2) === Number(2)); |
| 2142 | +console.log(Boolean(false) === Boolean(false)); |
| 2143 | +console.log(Symbol('foo') === Symbol('foo')); |
| 2144 | +``` |
| 2145 | + |
| 2146 | +- A: `true`, `true`, `false` |
| 2147 | +- B: `false`, `true`, `false` |
| 2148 | +- C: `true`, `false`, `true` |
| 2149 | +- D: `true`, `true`, `true` |
| 2150 | + |
| 2151 | +<details><summary><b>答案</b></summary> |
| 2152 | +<p> |
| 2153 | + |
| 2154 | +#### 答案: A |
| 2155 | + |
| 2156 | +每個 Symbol 都是完全唯一的。傳遞給 Symbol 的參數只是給 Symbol 的一個描述。 Symbol 的值不依賴於傳遞的參數。當我們建立兩個全新的 Symbol 去比較時:第一個`Symbol('foo')`,第二個`Symbol('foo')`, 因這兩個值是唯一的,彼此不相等,因此 `Symbol('foo') === Symbol('foo')` 會得到 `false`。 |
| 2157 | + |
| 2158 | +</p> |
| 2159 | +</details> |
| 2160 | + |
| 2161 | +--- |
| 2162 | + |
| 2163 | +###### 69. 將會輸出什麽內容? |
| 2164 | + |
| 2165 | +```javascript |
| 2166 | +const name = 'Lydia Hallie'; |
| 2167 | +console.log(name.padStart(13)); |
| 2168 | +console.log(name.padStart(2)); |
| 2169 | +``` |
| 2170 | + |
| 2171 | +- A: `"Lydia Hallie"`, `"Lydia Hallie"` |
| 2172 | +- B: `" Lydia Hallie"`, `" Lydia Hallie"` (`"[13x whitespace]Lydia Hallie"`, `"[2x whitespace]Lydia Hallie"`) |
| 2173 | +- C: `" Lydia Hallie"`, `"Lydia Hallie"` (`"[1x whitespace]Lydia Hallie"`, `"Lydia Hallie"`) |
| 2174 | +- D: `"Lydia Hallie"`, `"Lyd"`, |
| 2175 | + |
| 2176 | +<details><summary><b>答案</b></summary> |
| 2177 | +<p> |
| 2178 | + |
| 2179 | +#### 答案: C |
| 2180 | + |
| 2181 | +使用 `padStart` 函數,我們可以在字串的前面加上填充字串。傳遞給此函數的參數是字串的總長度(包含填充字串)。字串 Lydia Hallie 的長度為 `12` , 因此 `name.padStart(13)` 在字串的開頭只會插入1個空格,因為 12 + 1 等於 13。 |
| 2182 | + |
| 2183 | +如果傳給 `padStart` 函數的參數小於字串的長度,則不會加上填充字串。 |
| 2184 | + |
| 2185 | +</p> |
| 2186 | +</details> |
| 2187 | + |
| 2188 | +--- |
| 2189 | + |
| 2190 | +###### 70. 將會輸出什麽內容? |
| 2191 | + |
| 2192 | +```javascript |
| 2193 | +console.log('🥑' + '💻'); |
| 2194 | +``` |
| 2195 | + |
| 2196 | +- A: `"🥑💻"` |
| 2197 | +- B: `257548` |
| 2198 | +- C: 一個包含碼位(code point)的字串 |
| 2199 | +- D: 錯誤 |
| 2200 | + |
| 2201 | +<details><summary><b>答案</b></summary> |
| 2202 | +<p> |
| 2203 | + |
| 2204 | +#### 答案: A |
| 2205 | + |
| 2206 | +使用 `+` 運算元,你可以連接字串。在此例,我們將字串“🥑”與字串”💻“連接起來,產生”🥑💻“。 |
| 2207 | + |
| 2208 | +</p> |
| 2209 | +</details> |
| 2210 | + |
| 2211 | +--- |
| 2212 | + |
| 2213 | +###### 71. /* 1 */ 與 /* 2 */ 該填入什麼才能輸出 console.log 之後的值? |
| 2214 | + |
| 2215 | +```javascript |
| 2216 | +function* startGame() { |
| 2217 | + const answer = yield '你喜歡 JavaScript 嗎?'; |
| 2218 | + if (answer !== 'Yes') { |
| 2219 | + return "哦,我想我們該走了"; |
| 2220 | + } |
| 2221 | + return 'JavaScript 也愛你 ❤️'; |
| 2222 | +} |
| 2223 | + |
| 2224 | +const game = startGame(); |
| 2225 | +console.log(/* 1 */); // 你喜歡 JavaScript 嗎? |
| 2226 | +console.log(/* 2 */); // JavaScript 也愛你 ❤️ |
| 2227 | +``` |
| 2228 | + |
| 2229 | +- A: `game.next("Yes").value` and `game.next().value` |
| 2230 | +- B: `game.next.value("Yes")` and `game.next.value()` |
| 2231 | +- C: `game.next().value` and `game.next("Yes").value` |
| 2232 | +- D: `game.next.value()` and `game.next.value("Yes")` |
| 2233 | + |
| 2234 | +<details><summary><b>答案</b></summary> |
| 2235 | +<p> |
| 2236 | + |
| 2237 | +#### 答案: C |
| 2238 | + |
| 2239 | +`generator` 函數在遇到 yield 關鍵字時會 “暫停” 執行。首先,我們需要讓函數產生字串 "你喜歡 JavaScript 嗎?",這可以透過呼叫 `game.next().value` 來完成。 |
| 2240 | + |
| 2241 | +`startGame()` 函數會一行一行執行直到遇到 `yield` 關鍵字,在函數裡第一個就有一個 `yield` 關鍵字:所以執行到第一行就停止了! _此時answer變數還尚未定義_ |
| 2242 | + |
| 2243 | +當我們呼叫 `game.next("Yes").value`,前一個 `yield` 被傳遞給 `next()` 的參數值所取代。此例我們使用 `Yes`。變數 `answer` 的值現在等於 `Yes`。 if 語句的條件返回 `false`,並且會返回 `JavaScript 也愛你 ❤️` 。 |
| 2244 | + |
| 2245 | +</p> |
| 2246 | +</details> |
| 2247 | + |
| 2248 | +--- |
| 2249 | + |
| 2250 | +###### 72. 將會輸出什麽內容? |
| 2251 | + |
| 2252 | +```javascript |
| 2253 | +console.log(String.raw`Hello\nworld`); |
| 2254 | +``` |
| 2255 | + |
| 2256 | +- A: `Hello world!` |
| 2257 | +- B: `Hello` <br /> `world` |
| 2258 | +- C: `Hello\nworld` |
| 2259 | +- D: `Hello\n` <br /> `world` |
| 2260 | + |
| 2261 | +<details><summary><b>答案</b></summary> |
| 2262 | +<p> |
| 2263 | + |
| 2264 | +#### 答案: C |
| 2265 | + |
| 2266 | +`String.raw`會回傳一個字串,其中轉義符(`/n`, `/v`, `/t`等)被忽略! 反斜線可能是一個問題,因為你可能會有這樣的結果。 |
| 2267 | + |
| 2268 | +`const path = "C:\Documents\Projects\table.html"`。 |
| 2269 | + |
| 2270 | +將會得到: |
| 2271 | + |
| 2272 | +`C:DocumentsProjects able.html` |
| 2273 | + |
| 2274 | +如果使用`String.raw`,它將直接忽略轉譯並輸出。 |
| 2275 | + |
| 2276 | +`C:\Documents\Projects\table.html`。 |
| 2277 | + |
| 2278 | +在這種情況下,字串會以 "Hello\nworld",被記錄下來。 |
| 2279 | + |
| 2280 | +</p> |
| 2281 | +</details> |
| 2282 | + |
| 2283 | +--- |
| 2284 | + |
| 2285 | +###### 73. 將會輸出什麽內容? |
| 2286 | + |
| 2287 | +```javascript |
| 2288 | +async function getData() { |
| 2289 | + return await Promise.resolve('I made it!'); |
| 2290 | +} |
| 2291 | + |
| 2292 | +const data = getData(); |
| 2293 | +console.log(data); |
| 2294 | +``` |
| 2295 | + |
| 2296 | +- A: `"I made it!"` |
| 2297 | +- B: `Promise {<resolved>: "I made it!"}` |
| 2298 | +- C: `Promise {<pending>}` |
| 2299 | +- D: `undefined` |
| 2300 | + |
| 2301 | +<details><summary><b>答案</b></summary> |
| 2302 | +<p> |
| 2303 | + |
| 2304 | +#### 答案: C |
| 2305 | + |
| 2306 | +一個異步函數總是返回一個 promise 。 `await` 仍然要等待 promise 的 resolve:當我們呼叫 `getData()` 等於 `data` 時,會得到一個等待的 promise。 |
| 2307 | + |
| 2308 | +如果我們想獲取 resolve 後的值`"I made it"`,我們可以在`data`上使用`.then()`函數: |
| 2309 | + |
| 2310 | +`data.then(res => console.log(res))`。 |
| 2311 | + |
| 2312 | +這樣就會出現 `"I made it!"` 的記錄。 |
| 2313 | + |
| 2314 | +</p> |
| 2315 | +</details> |
| 2316 | + |
| 2317 | +--- |
| 2318 | + |
| 2319 | +###### 74. 將會輸出什麽內容? |
| 2320 | + |
| 2321 | +```javascript |
| 2322 | +function addToList(item, list) { |
| 2323 | + return list.push(item); |
| 2324 | +} |
| 2325 | + |
| 2326 | +const result = addToList('apple', ['banana']); |
| 2327 | +console.log(result); |
| 2328 | +``` |
| 2329 | + |
| 2330 | +- A: `['apple', 'banana']` |
| 2331 | +- B: `2` |
| 2332 | +- C: `true` |
| 2333 | +- D: `undefined` |
| 2334 | + |
| 2335 | +<details><summary><b>答案</b></summary> |
| 2336 | +<p> |
| 2337 | + |
| 2338 | +#### 答案: B |
| 2339 | + |
| 2340 | +`.push()`函數回傳的是陣列的長度!原本陣列包含一個元素(字串`"香蕉"`),長度為`1`。後來將字串 `"apple"` 加到陣列中後,陣列包含兩個元素。所以會從`addToList`函數中得到,長度為 `"2"`。 |
| 2341 | + |
| 2342 | +`push`函數修改了原來的陣列。如果你想從函數中返回 _陣列_ 而不是 _陳列的長度_ ,你應該在加完`item`到陣列後,回傳`list`。 |
| 2343 | + |
| 2344 | +</p> |
| 2345 | +</details> |
| 2346 | + |
| 2347 | +--- |
| 2348 | + |
| 2349 | +###### 75. 將會輸出什麽內容? |
| 2350 | + |
| 2351 | +```javascript |
| 2352 | +const box = { x: 10, y: 20 }; |
| 2353 | + |
| 2354 | +Object.freeze(box); |
| 2355 | + |
| 2356 | +const shape = box; |
| 2357 | +shape.x = 100; |
| 2358 | + |
| 2359 | +console.log(shape); |
| 2360 | +``` |
| 2361 | + |
| 2362 | +- A: `{ x: 100, y: 20 }` |
| 2363 | +- B: `{ x: 10, y: 20 }` |
| 2364 | +- C: `{ x: 100 }` |
| 2365 | +- D: `ReferenceError` |
| 2366 | + |
| 2367 | +<details><summary><b>答案</b></summary> |
| 2368 | +<p> |
| 2369 | + |
| 2370 | +#### 答案: B |
| 2371 | + |
| 2372 | +`Object.freeze` 使我們無法增加、刪除或修改Object的屬性(除非該屬性的值是另一個Object)。 |
| 2373 | + |
| 2374 | +當我們建立變數`shape`並等同被凍結的Object`box`時,`shape`也是指一個被凍結的Object。你可以透過使用`Object.isFrozen`檢查一個Object是否被凍結。在這種情況下,`Object.isFrozen(shape)`回傳true,因為變數`shape`也指向一個凍結Object。 |
| 2375 | + |
| 2376 | +由於`shape`是被凍結的,而且`x`的值不是一個Object,所以我們不能修改`x`的屬性。 `x`仍然等於`10`,於是`{ x: 10, y: 20 }`被記錄下來。 |
| 2377 | + |
| 2378 | +</p> |
| 2379 | +</details> |
| 2380 | + |
| 2381 | +--- |
0 commit comments