Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get list of methods in JavaScript's Array

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.

like image 460
pitr Avatar asked Dec 08 '10 19:12

pitr


2 Answers

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.

like image 174
casablanca Avatar answered Sep 20 '22 12:09

casablanca


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
like image 24
Andrew Hare Avatar answered Sep 22 '22 12:09

Andrew Hare