是否可以以跨浏览器友好的方式遍历所有JavaScript Array.prototype
函数名称?我知道这适用于IE9 +和现代浏览器:
var names = Object.getOwnPropertyNames(Array.prototype);
names.forEach(function(name) {
console.log(name); // function name
});
有没有办法在IE8和IE中获得相同的列表? IE7?我试过了:
for(var key in Array.prototype) {
console.log(key); // undefined
}
答案 0 :(得分:1)
如果您试图在版本9之前找到IE浏览器支持的内容,您可以假设它是IE9列表的一个子集,并且可以获取不支持的列表。
这是您在#9之前在IE中获得的列表:
concat,constructor,join,length,pop,push,reverse,shift,slice,sort,splice,toLocaleString,toString,unshift
你可以测试一下 -
<!doctype html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>Small Page</title>
<style>
</style>
<script>
onload= function(){
var testnames= ['concat', 'constructor', 'every', 'filter', 'forEach',
'indexOf', 'join', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce',
'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice',
'toLocaleString', 'toString', 'unshift'],
L= 23;
while(L){
if(!(testnames[--L]in Array.prototype)) testnames.splice(L, 1);
}
document.getElementsByTagName('textarea')[0].value= testnames;
}
</script>
</head>
<body>
<p> <textarea rows="8" cols="60"> </textarea> </p>
</body>
</html>