开发者

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开发者_JS百科", "pop", "push", etc. Obviously for(m in Array.prototype){console.log(m)} won't work.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜