15

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?

3 Answers 3

27

do you mean:

var arrObj = Object.getOwnPropertyNames(Array.prototype);
for( var funcKey in arrObj ) {
   console.log(arrObj[funcKey]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, exactly what I was searching for. Thanks.
Do note that arrObj is actually an array and looping an array with for..in has it's drawbacks.
Sudhir this doesnt return the prototype property names when you pass an instance of the prototype eg Object.getOwnPropertyNames(myArr); is this possible?
@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.
2

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

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

Comments

-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);

6 Comments

This don't work. (in Google chrome), as I put in question, Array.prototype is empty array when access it in this way.
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.
Do you mean you don't see the property in the console?
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
@xdazz Your demo loads mootools, and it outputs only the methods added to Array.prototype by mootools mootools.net/docs/core/Types/Array. (getRandom is listed, but map isn't.)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.