I wrote this simple code to remove all text inside of a h2 element:
const h2 = document.querySelector('h2');
const children = Array.from(h2.childNodes)
.filter((childNode)=>childNode.nodeName === '#text');
children.forEach(h2.removeChild); // Throws TypeError: Illegal invocation
However, the last line threw an error - TypeError: Illegal invocation. This was fixed when I replaced the last line with:
children.forEach((child)=>h2.removeChild(child)); // Just works
My question is, why did javascript throw the error in the first place and why did the arrow function fix it? h2.removeChild is a function that takes 1 argument, so shouldn't ( h2.removeChild ) act identically to ( (child)=>h2.removeChild(child) ) ?