6
var c=$('<canvas></canvas>')[0].getContext('2d')
for(m in c){console.log(m)}

This prints a list of methods in CanvasRenderingContext2D. How can I do the same for an Array. I want to get "splice", "pop", "push", etc. Obviously for(m in Array.prototype){console.log(m)} won't work.

3 Answers 3

14

Most methods and properties of built-in objects are internally marked as non-enumerable, so they will not be enumerated in a for-in loop.

ECMAScript 5 has an Object.getOwnPropertyNames method that returns an array of all property names, so you can do:

Object.getOwnPropertyNames(Array.prototype)

but this isn't supported by all browsers yet.

Sign up to request clarification or add additional context in comments.

Comments

2

Do this:

for (m in Array) {
    console.log(m)
}

Output:

from
type
implement
extend
alias
mirror
$family
$constructor
pop
push
reverse
shift
sort
splice
unshift
concat
join
slice
indexOf
lastIndexOf
filter
forEach
every
map
some
reduce
reduceRight
each
clone
invoke
clean
associate
link
contains
append
getLast
getRandom
include
combine
erase
empty
flatten
pick
hexToRgb
rgbToHex
overloadSetter
overloadGetter
hide
protect
apply
call
attempt
pass
delay
periodical
create
bind
bindWithEvent
run

6 Comments

This isn't the same as getting the properties on an Array instance/prototype, though, which are non-enumerable. It's coincidental that many Array.x properties are named the same as Array.prototype.x properties.
Where did you get this output from anyhow? It doesn't work for me on FF4. The only standard property of Array is Array.isArray and that is once again non-enumerable.
You may want to add an if (typeof m === 'function') to that, so you're getting only functions.
Doesn't work for me on Chrome, IE8, FF 3.6. I have no idea how you got that output. Array constructor has no properties as far as I can test.
I am using Chrome 9.0.597.10 dev.
|
1

I have no idea how to do it with plain js. I usually have underscorejs loaded ant it have an it have a function that returns all the functions of an object

http://documentcloud.github.com/underscore/#functions

You could check underscorejs code to check how they do it.

Comments

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.