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 работает
forEachwas intentionally added not so long ago. And not supported by all browsers - DmytrykchildNodesinstance of theNodeListclass? - DiDex.childNodesis a method that every Node has. TheNode.childNodesmethod returns aNodeListcollection, which is not an array, but an object. TheNodeListprototypeNodeListlooks like this:myNodeList --> NodeList.prototype --> Object.prototype --> null. There is only one method in theNodeList.prototypeobject:item. Knowledge Source: developer.mozilla.org/ru/docs/Web/API/Node/childNodes - Vlad ZhadchenkoNodeListrefers to theNodeList.prototype, which defines its ownforEach(). - Vlad Zhadchenko