iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 17
0
自我挑戰組

利用30分鐘~想一個前端問題系列 第 17

利用30分鐘~想一個前端問題 Day17--show

show

Shows all the elements specified.

Use the spread operator (...) and Array.prototype.forEach() to clear the display property for each element specified.

const show = (...el) => [...el].forEach(e => (e.style.display = ''));
//EXAMPLES
show(...document.querySelectorAll('img')); // Shows all <img> elements on the page

顯示所有指定的元素

用 展開運算 (...) 和 Array.prototype.forEach() 清楚所有指定元素的 display 屬性

分析點

  1. ... 展開運算符 (spread)

展開運算符(Spread Operator)是把一個陣列展開(expand)成個別值,這個運算符後面必定接著一個陣列。最常見的是用來組合(連接)陣列,對應的陣列方法是concat,以下是一個簡單的範例:

const params = [ "hello", true, 7 ]
const other = [ 1, 2, ...params ] // [ 1, 2, "hello", true, 7 ]

也可以做成淺拷貝

const arr = [1,2,3]
const arr2 = [...arr]

arr2.push(4) //不會影響到arr
  1. Array.prototype.forEach()

用 forEach() 方法來遍歷陣列中的每一個元素

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

參考資料

MDN ... 展開運算符

ES6 入门教程


上一篇
利用30分鐘~想一個前端問題 Day16-similarity
下一篇
利用30分鐘~想一個前端問題 Day18-pull
系列文
利用30分鐘~想一個前端問題30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言