简体   繁体   中英

How can I iterate over Array.prototype functions

I want to wrap all Array functions in array object, but in console

>>> Array.prototype
[]
>>> [].prototype
undefined

but when I type Array.prototype in console it show all functions in autocomple, how can I get those functions? Where are they hidden?

do you mean:

var arrObj = Object.getOwnPropertyNames(Array.prototype);
for( var funcKey in arrObj ) {
   console.log(arrObj[funcKey]);
}

Using ECMAScript 6 (ECMAScript 2015), you can simplify a bit:

for (let propName of Object.getOwnPropertyNames(Array.prototype)) {
   console.log(Array.prototype[propName]);
}
var proto = Array.prototype;

for (var key in proto) {
    if (proto.hasOwnProperty(key)) {
        console.log(key + ' : ' + proto[key]);
    }
}

demo.

And if you want to check its property in console.

Use: console.dir(Array.prototype);

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