简体   繁体   English

如何迭代Array.prototype函数

[英]How can I iterate over Array.prototype functions

I want to wrap all Array functions in array object, but in console 我想将所有Array函数包装在数组对象中,但是在控制台中

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

but when I type Array.prototype in console it show all functions in autocomple, how can I get those functions? 但是当我在控制台中键入Array.prototype它显示自动填充中的所有函数时,我该如何获得这些函数? 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: 使用ECMAScript 6(ECMAScript 2015),您可以简化一下:

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); 使用: console.dir(Array.prototype);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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