简体   繁体   中英

List all Array.prototype functions in IE8-

Is it possible to loop through all JavaScript Array.prototype function names in a cross-browser friendly way? I know this works in IE9+ and modern browsers:

var names = Object.getOwnPropertyNames(Array.prototype);
names.forEach(function(name) {
    console.log(name); // function name
});   

Is there a way to get the same list in IE8 & IE7? I tried:

for(var key in Array.prototype) {
    console.log(key); // undefined
}

If you are trying to find what is supported in an IE browser before version 9, you can assume that it is a subset of IE9's list and winnow out the ones not supported.

This is the list you get in IE before #9:

concat, constructor, join, length, pop, push, reverse, shift, slice, sort, splice, toLocaleString, toString, unshift

You can test it-

<!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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM