"Array.prototype.slice:“这不是JavaScript对象"" IE8中的错误 [英] "Array.prototype.slice: 'this' is not a JavaScript object" error in IE8

查看:123
本文介绍了"Array.prototype.slice:“这不是JavaScript对象"" IE8中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,IE8可以访问Array.prototype.slice方法.但是,当我尝试调用它以将NodeList转换为数组时,它给了我错误Array.prototype.slice: 'this' is not a JavaScript object. 您可以在此处查看 ,或在此处查看我的代码:

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 gives me the error Array.prototype.slice: 'this' is not a JavaScript object. You can check it out here, or look at my code here:

HTML

<div id="test">Test</div>

JavaScript

var divs = document.getElementsByTagName('div');
divs = Array.prototype.slice.call(divs);
console.log(divs);

这是怎么回事?

推荐答案

更新:A

Update: A NodeList can be treated as an array in some ways - you don't actually have to do anything special with it before you can loop over it, for example:

var aDivs = [];
for (var = i = 0; i < divs.length; i++) {
    aDivs.push(divs[i]);
}

这将创建一个数组,其中包含您运行document.getElementsByTagName()

This will create an array with all of the nodes that matched when you ran document.getElementsByTagName()

请参见此问题有关为什么slice在某些浏览器中与NodeList一起使用但在其他浏览器中不起作用的完整说明,但这使该句子与规范无关:

See this question for a full explanation of why slice works with a NodeList in some browsers but not others, but it boils down this this sentence from the specification:

slice函数能否成功应用于宿主对象取决于实现.

Whether the slice function can be applied successfully to a host object is implementation-dependent.

这篇关于"Array.prototype.slice:“这不是JavaScript对象"" IE8中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆