---
# System prepended metadata

title: 【JS】小知識 - foreach(functionName)

---

# 【JS】小知識 - foreach(functionName) 

* 原來foreach後面可以包一個function呢
* ~~大驚小怪~~

```javascript

const numbers = [45, 4, 9, 16];
let txt = "";
numbers.forEach(myFunction);
document.getElementById("demo").innerHTML = txt;

function myFunction(value, index, array) {
  txt += value + "<br>"; 
}
//
45
4
9
16

```
---
# 【JS】小知識 - for / in 是什麼？
* for…in用來迭代Object中enumerable的屬性
* 遞迴該Object的prototype中enumerable的屬性

```javascript

Object.prototype.objCustom = function() {}
Array.prototype.arrCustom = function() {}

const iterable = [3, 5, 7]
iterable.foo = 'hello'

for (const i in iterable) {
  console.log(i)
}
// 0, 1, 2, "foo", "arrCustom", "objCustom"

```