document.body.childNodes is an object that inherits all its properties from the Object.prototype . At the same time, it is not an array, because

Array.isArray(document.body.childNodes) = false .

But then how does it inherit the Array.prototype.forEach property?

 console.log(Array.isArray(document.body.childNodes)); // childNodes - не массив console.log(typeof document.body.childNodes); console.log(document.body.childNodes.hasOwnProperty("forEach")); // в самом childNodes forEach метода нет console.log("forEach" in document.body.childNodes); // но у него есть метод массива Array.prototype.forEach document.body.childNodes.forEach(function(){ console.log("go forEach") }) // тем не менее forEach работает 

  • one
    I read somewhere that forEach was intentionally added not so long ago. And not supported by all browsers - Dmytryk
  • one
    Can a childNodes instance of the NodeList class? - DiDex
  • .childNodes is a method that every Node has. The Node.childNodes method returns a NodeList collection, which is not an array, but an object. The NodeList prototype NodeList looks like this: myNodeList --> NodeList.prototype --> Object.prototype --> null . There is only one method in the NodeList.prototype object: item . Knowledge Source: developer.mozilla.org/ru/docs/Web/API/Node/childNodes - Vlad Zhadchenko
  • from where the assumption. What is an array method , and not just a method? - Grundy
  • Indeed, using the debager you can determine that our NodeList refers to the NodeList.prototype , which defines its own forEach() . - Vlad Zhadchenko

0