14

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?

jcubic
  • 61,973
  • 54
  • 229
  • 402

3 Answers3

25

do you mean:

var arrObj = Object.getOwnPropertyNames(Array.prototype);
for( var funcKey in arrObj ) {
   console.log(arrObj[funcKey]);
}
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
  • Yes, exactly what I was searching for. Thanks. – jcubic Sep 12 '12 at 10:08
  • Do note that `arrObj` is actually an array and [looping an array with for..in](http://stackoverflow.com/questions/3010840/loop-through-array-in-javascript) has it's drawbacks. – A1rPun Apr 15 '16 at 12:30
  • Sudhir this doesnt return the prototype property names when you pass an instance of the prototype eg `Object.getOwnPropertyNames(myArr);` is this possible? – wal Jun 01 '17 at 00:15
  • @wal you should be able to use `Object.getOwnPropertyNames(myArr.__proto__)`. I know this is an old question but someone just upvoted my question and I've checked the answers. – jcubic Aug 11 '23 at 17:46
2

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

for (let propName of Object.getOwnPropertyNames(Array.prototype)) {
   console.log(Array.prototype[propName]);
}
Peter Tseng
  • 13,613
  • 4
  • 67
  • 57
-2
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);

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • This don't work. (in Google chrome), as I put in question, Array.prototype is empty array when access it in this way. – jcubic Sep 12 '12 at 10:05
  • It don't, that's why I ask this question. `console.dir(Array.prototype);` return array[0] with those functions, but iterating over Array.prototype don't work. – jcubic Sep 12 '12 at 10:11
  • Do you mean you don't see the property in the console? – xdazz Sep 12 '12 at 10:13
  • No, did you? Chrome, Opera and Firefix don't show them (console.dir work but only in console, and in Opera it look like you call that function when you type Array.prototype), you can only iterate over prototype content that you create by yourself, but not default functions that are put into prototype by browser like `Array.prototype.pop`. Aditionaly `[].prototype` is undefined to get prototype of Array object I need to use `[].constructor.prototype` – jcubic Sep 13 '12 at 12:51
  • @xdazz Your demo loads mootools, and it outputs only the methods added to Array.prototype by mootools http://mootools.net/docs/core/Types/Array. (`getRandom` is listed, but `map` isn't.) – mjs Jan 02 '14 at 08:59
  • It does not work for me either in 2019 using firefox. – Andrew S Aug 24 '19 at 21:09