“Array.prototype.slice: 'this' is not a JavaScript object” error in IE8

前端 未结 3 1129
名媛妹妹
名媛妹妹 2021-01-12 01:59

It is my understanding that IE8 has access to the Array.prototype.slice method. Yet when I try to call it to turn a NodeList into an array, it give

3条回答
  •  执笔经年
    2021-01-12 02:33

    I assume that you want to keep the same content even if the NodeList set changes.

    If it's that case, bad news : IE8 is broken. And it can't handle using slice on NodeList.

    So you will need to use a fallback and make the "slice" yourself when slice fails (by using a try/catch).

    Note that If you don't expect the DOM to change, and if an array-like object is enough, then you can just use the NodeList like any other array (except that it is not, and that perhaps it will be modified if the DOM changes).

    [edit] Actually it's not a broken design, it's allowed by the standard (as stated by the link in Kelvin Mackay's comment)

提交回复
热议问题