IE supports forEach(…) when invoked fromthe console but not when called from the code

前端 未结 3 1655
死守一世寂寞
死守一世寂寞 2021-01-12 06:57

I\'m running this snippet the console. In IE it produces the output just as expected. Running the same in Cr and FF for reference confirms the congruence of behavior.

<
3条回答
  •  既然无缘
    2021-01-12 07:13

    Little late, but it might be useful if someone has the same problem and doesn't want/can't replace all forEach methods with [].forEach.call(elements, fn(el)). Here is polyfill that works for ie11

    if (! Object.getOwnPropertyDescriptor(NodeList.prototype, 'forEach')) {
        Object.defineProperty(NodeList.prototype, 'forEach', Object.getOwnPropertyDescriptor(Array.prototype, 'forEach'));
    }
    

提交回复
热议问题