最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

get list of methods in JavaScript's Array - Stack Overflow

programmeradmin3浏览0评论
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.

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.

Share Improve this question asked Dec 8, 2010 at 19:42 pitrpitr 7131 gold badge8 silver badges25 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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.

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
bine
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./underscore/#functions

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

发布评论

评论列表(0)

  1. 暂无评论